首页 > 解决方案 > 如何通过组件编辑存储中的数据?反应 JS

问题描述

我已经在我的商店中记录了:false,我需要在 react js 的组件中的按钮操作上将其更改为 true。如果您能告诉我如何编辑/更新商店中的数据或共享链接?

标签: reactjscomponentscontainersstore

解决方案


如何创建商店

const redux = require('redux');
const createStore = redux.createStore;

const initialState = {
    counter: 0
}

const rootReducer = (state = initialState, action) => {
  if(action.type === 'INC_COUNTER'){
    return {
      ...state,
      counter: state.counter + 1
    };
  }

  return state;
};

如何调用操作

const mapDispatchToProps = dispatch => {
  return {
    onIncrementCounter: () => dispatch({type:'INC_COUNTER'})
  };
};

单击时调用它

<button clicked={this.props.onIncrementCounter} />

推荐阅读