在reducer内部切换

时间:2019-03-01 04:54:16

标签: reactjs react-redux reducers

在我的reducer文件中,创建的动作是

[actions.toggle]: state => ({
      ...state,
      move: false,
    }),

在jsx文件中,我的构造函数是

 constructor(props){
    super(props);
    store.dispatch(actions.toggle());
    this.state ={
    enableMove: store.getState.move
    }
 }

我还创建了一个用于切换的功能

 toggleShowMove(){
 this.setState({
    enableMove: !this.state.enableMove,
 })
 }

在我的渲染器内

 <button onClick={() => this.toggleShowMove()}></button>

<div>{this.state.enableMove && <div>Hello</div>}</div>

切换开关完美工作。但我想更改reducer内的切换值。如何在reducer内切换属性?

1 个答案:

答案 0 :(得分:1)

您只需要使用化简器中state的值并将其切换,而不是将move设置为false

[actions.toggle]: state => ({
  ...state,
  move: !Boolean(state.move),
}),
相关问题