错误:redux

编程入门 行业动态 更新时间:2024-10-05 11:22:54
错误:redux-thunk this.props.dispatch不是函数[重复](Error :redux-thunk this.props.dispatch is not function [duplicate])

这个问题在这里已经有了答案:

如何将多个actionCreators包装成一个道具? 3个答案

当我点击按钮时,我得到了错误

未捕获的TypeError:_this2.props.dispatch不是函数

const reducers = combineReducers({ geod:reducer }); function configureStore(initialState = {}) { const store = createStore( reducers, initialState, applyMiddleware(thunk) ) return store; }; var ping = function ping(store) { return function (next) { return function (action) { //EX1 /* if(action.type !== 'ACTIVATE_GEOD') return next(action) console.log(`Тип события: ${action.type}, события: ${action}`) return next(action); */ //EX2 console.log(`Тип события: ${action.type}, события: ${action}`) return next(action); }; }; }; window.store = configureStore(); store.subscribe(() => console.log(store.getState())) function logout(){ return function(dispatch){ setTimeout(function(){ activateGeod({title:'6 sec'}) },6000) } } // App.js class App extends React.Component { render() { return ( <div> PAGE: <h1>{this.props.geod.title || 'Hello World!'}</h1> {this.props.geod.title ? <button onClick={this.props.closeGeod}> Exit Geod </button> : <button onClick={() => this.props.activateGeod({ title: 'I am a geo dude!' })}> Click Me! </button> } <button onClick={() => this.props.dispatch( logout() ) }> THUNK REDUX Click Me! </button> </div> ); } } // AppContainer.js const mapStateToProps = (state, ownProps) => ({ geod: state.geod }); /* const mapDispatchToProps = { activateGeod, closeGeod, }; */ const mapDispatchToProps = (dispatch) => { return { activateGeod: bindActionCreators(activateGeod, dispatch), closeGeod: bindActionCreators(closeGeod, dispatch), } }; const AppContainer = connect( mapStateToProps, mapDispatchToProps )(App); ReactDom.render( <Provider store={store}> <AppContainer /> </Provider>, document.getElementById('wrapper') );

请帮帮我

This question already has an answer here:

How to wrap multi actionCreators into one props? 3 answers

When I clicked into button I got Error

Uncaught TypeError: _this2.props.dispatch is not a function

const reducers = combineReducers({ geod:reducer }); function configureStore(initialState = {}) { const store = createStore( reducers, initialState, applyMiddleware(thunk) ) return store; }; var ping = function ping(store) { return function (next) { return function (action) { //EX1 /* if(action.type !== 'ACTIVATE_GEOD') return next(action) console.log(`Тип события: ${action.type}, события: ${action}`) return next(action); */ //EX2 console.log(`Тип события: ${action.type}, события: ${action}`) return next(action); }; }; }; window.store = configureStore(); store.subscribe(() => console.log(store.getState())) function logout(){ return function(dispatch){ setTimeout(function(){ activateGeod({title:'6 sec'}) },6000) } } // App.js class App extends React.Component { render() { return ( <div> PAGE: <h1>{this.props.geod.title || 'Hello World!'}</h1> {this.props.geod.title ? <button onClick={this.props.closeGeod}> Exit Geod </button> : <button onClick={() => this.props.activateGeod({ title: 'I am a geo dude!' })}> Click Me! </button> } <button onClick={() => this.props.dispatch( logout() ) }> THUNK REDUX Click Me! </button> </div> ); } } // AppContainer.js const mapStateToProps = (state, ownProps) => ({ geod: state.geod }); /* const mapDispatchToProps = { activateGeod, closeGeod, }; */ const mapDispatchToProps = (dispatch) => { return { activateGeod: bindActionCreators(activateGeod, dispatch), closeGeod: bindActionCreators(closeGeod, dispatch), } }; const AppContainer = connect( mapStateToProps, mapDispatchToProps )(App); ReactDom.render( <Provider store={store}> <AppContainer /> </Provider>, document.getElementById('wrapper') );

Help me please

最满意答案

const mapDispatchToProps = (dispatch) => { return { activateGeod: bindActionCreators(activateGeod, dispatch), closeGeod: bindActionCreators(closeGeod, dispatch), } };

应该

const mapDispatchToProps = (dispatch) => ({ activateGeod: bindActionCreators(activateGeod, dispatch), closeGeod: bindActionCreators(closeGeod, dispatch), logout: () => dispatch(logout()), });

然后你可以在组件中调用this.props.logout()而不是dispatch(logout())

const mapDispatchToProps = (dispatch) => { return { activateGeod: bindActionCreators(activateGeod, dispatch), closeGeod: bindActionCreators(closeGeod, dispatch), } };

should be

const mapDispatchToProps = (dispatch) => ({ activateGeod: bindActionCreators(activateGeod, dispatch), closeGeod: bindActionCreators(closeGeod, dispatch), logout: () => dispatch(logout()), });

Then you can just call this.props.logout() instead dispatch(logout()) in your component

更多推荐

本文发布于:2023-08-04 21:57:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422205.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   redux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!