【发布时间】:2017-12-15 17:58:12
【问题描述】:
了解您可以使用以下方法创建动态路由:
<Route path="/:category" component={PostsContainer} />
class PostsContainer extends Component {
render() {
<p>{this.props.match.params.category}</p>
}
}
所以我的问题是如何通过 api 调用向调度添加动态路由?
我想做一些事情,比如在 a 类中,向 a 类发送 api 调用,然后派发……而在 b 类中,向 b 类发送 api 调用,然后派发……
export const getPostsAction = (match) => (dispatch) => {
getPosts(category).then((posts) => {
dispatch({
type: postsConst.GET_ALL_POSTS_SUCCESS,
posts,
});
});
};
问题是我不能将 componentDidMount 与 this.props.getPostsAction(category) 一起使用,它只会发生一次,即使我点击不同的 /categories... 或者我应该从 componentWillRecieveProps 更新,但我是不太确定什么是最好的方法...
【问题讨论】:
-
API 调用 A 类与 B 类有何不同
-
你能举一个你想要调度的动作类型的例子吗?它有什么动态?
-
添加了动作。所以 getPosts 会有类似 /api/category/${category}
-
你知道我半小时前把这个放在我的回答里了 :)
标签: reactjs react-router react-redux react-router-v4