jan
jan

Reputation: 221

toggle inside reducer In react

In my reducer file, created actions which is

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

In jsx file , my constructor is

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

I also created a function to work with toggle

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

Inside my render

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

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

The toggle works perfectly. But I want to change toggle value inside reducer. How can I toggle property inside reducer?

Upvotes: 1

Views: 384

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 281726

You simply need to use the value from state inside reducer and toggle it instead of setting move to false

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

Upvotes: 2

Related Questions