在减速器内部切换在反应中
toggle inside reducer In react
在我的减速器文件中,创建的动作是
[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 内部状态的值并切换它而不是将 move
设置为 false
[actions.toggle]: state => ({
...state,
move: !Boolean(state.move),
}),
在我的减速器文件中,创建的动作是
[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 内部状态的值并切换它而不是将 move
设置为 false
[actions.toggle]: state => ({
...state,
move: !Boolean(state.move),
}),