使用react-router-relay访问要在查询中使用的url参数

时间:2016-09-15 16:36:09

标签: reactjs react-router relayjs relay react-router-relay

我正在使用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')}
      }
    }
  `
};

1 个答案:

答案 0 :(得分:1)

应该这么简单:

import Relay from 'react-relay';

export default {
  creative: () => Relay.QL`query { creative(id: $id)  }`
};

然后,您可以通过组件的id访问this.props