我正在使用react-router和react-router-relay来处理我的应用程序上的路由。我在尝试访问url路径中传递的id参数时遇到问题。这样做的正确方法是什么?
routes.js
import CreativeQuery from './CreativeQuery';
<Route path='/creative' component={CreativeComponent} queries={ViewerQuery}>
<Route path="/creative/edit/:id" component={CreativeEditComponent} queries={CreativeQuery}/>
CreativeQuery.js
import Relay from 'react-relay';
export default {
creative: (Component) => Relay.QL`
query {
creative(id:$id) { #<--- id from the url param
${Component.getFragment('creative')}
}
}
`
};
答案 0 :(得分:1)
应该这么简单:
import Relay from 'react-relay';
export default {
creative: () => Relay.QL`query { creative(id: $id) }`
};
然后,您可以通过组件的id
访问this.props
。