React-Redux无法触发事件:点击按钮无法更新Reducer状态求助
问题原因及解决方案
你的问题出在没有正确调用action创建函数,导致Redux没有收到有效的action对象来触发状态更新。
具体问题点
在mapDispatchToProps中,你传递给dispatch的是fetchUsersSuccess函数本身,而不是它调用后返回的action对象:
const mapDispatchToProps=dispatch=>{ return { handleSubmit: () => {dispatch(fetchUsersSuccess)} // 这里没有调用函数! } }
fetchUsersSuccess是一个action创建函数,它需要被执行(加上())才能返回包含type和payload的标准action对象——Redux只有收到这样的对象,才会触发reducer执行状态更新逻辑。
修正方案
只需要在fetchUsersSuccess后面加上括号,调用这个函数获取action对象后再dispatch:
const mapDispatchToProps=dispatch=>{ return { handleSubmit: () => {dispatch(fetchUsersSuccess())} // 调用函数生成action } }
额外优化建议
你也可以用React-Redux提供的简化写法,直接把action创建函数作为对象传给connect的第二个参数,React-Redux会自动帮你绑定dispatch:
// 去掉mapDispatchToProps,直接传入action创建函数 export default connect(mapStateToProps, { handleSubmit: fetchUsersSuccess })(App)
额外检查点
如果修正后状态还是没更新,建议确认:
- 是否在应用根组件外层用
Provider包裹了App,并传入了正确创建的Store:
这是React-Redux生效的必要前提,确保所有连接的组件都能访问到Store。import { Provider } from 'react-redux'; import { createStore } from 'redux'; import reducer from './reducer'; const store = createStore(reducer); ReactDOM.render( <Provider store={store}> <App /> </Provider>, document.getElementById('root') );
内容的提问来源于stack exchange,提问作者aarti
相关产品推荐
相关产品推荐

