You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:
    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')
    );
    
    这是React-Redux生效的必要前提,确保所有连接的组件都能访问到Store。

内容的提问来源于stack exchange,提问作者aarti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 00:07:39