Javascript 使用react路由器,redux将无法工作
我读过很多类似的问题,没有一个适合我 关于 索引文件,所有路由嵌套在提供程序中Javascript 使用react路由器,redux将无法工作,javascript,reactjs,react-router,redux,redux-thunk,Javascript,Reactjs,React Router,Redux,Redux Thunk,我读过很多类似的问题,没有一个适合我 关于 索引文件,所有路由嵌套在提供程序中 //entry file let routes = ( <Route path="/" component={UserHome}></Route> ); ReactDom.render( <Provider store={store} > <ReduxRouter> {routes} <
//entry file
let routes = (
<Route path="/" component={UserHome}></Route>
);
ReactDom.render(
<Provider store={store} >
<ReduxRouter>
{routes}
</ReduxRouter>
</Provider>,
rootElement
);
//条目文件
让路线=(
);
ReactDom.render(
{routes}
,
根元素
);
组件。我记录了this.props,没有dispatch属性。上下文的参数是一个空对象
//user_home.js
class UserHome extends React.Component {
constructor(props, context){
//context is empty Object
super(props, context);
}
render() {
//this.props.dispatch is undefined
return (
<div className="container">
test
</div>
);
}
}
function mapStateToProps(state){
return state;
}
export default connect(mapStateToProps, {
pushState
})(UserHome);
//user_home.js
类UserHome扩展了React.Component{
构造函数(道具、上下文){
//上下文是空对象
超级(道具、背景);
}
render(){
//此.props.dispatch未定义
返回(
测试
);
}
}
函数MapStateTops(状态){
返回状态;
}
导出默认连接(MapStateTops{
pushState
})(用户主页);
也许,这是一个简单的问题,但我花了很多时间。希望能得到你的帮助。谢谢大家! 也许我们可以注入动作创建者来连接 行动
//action file
export function xxAction(){
return dispatch =>{
dispatch({
type: 'XX_ACTION'
});
}
}
此解决方案适用于在子组件中分派操作。存在一些限制,期待更好的解决方案。您好,何处。基本上,对于您的方法,您需要使用
redux-thunk
将调度传递给action creator,对于简单的情况来说,这似乎有些过分。您有mapDispatchToProps
功能作为第二个connect
参数,它的设计目的是将动作创建者绑定到dispatch
,并将它们进一步传递给连接组件的道具。在context
上发言,除非您在子组件上指定contextTypes
字段,否则您的组件将无法访问它。
import * as ActionCreators from './actions/user_actions';
//we can use the actions in ActionCreators to dispatch the action.
export default connect(mapStateToProps, {
pushState: pushState,
...ActionCreators
})(Component);
//action file
export function xxAction(){
return dispatch =>{
dispatch({
type: 'XX_ACTION'
});
}
}