What is the difference between:
this.setState(produce((draft) => { draft.name ='name'}
and
this.setState(produce(this.state, (draft) => { draft.name ='name'}
can someone explain me this?
What is the difference between:
this.setState(produce((draft) => { draft.name ='name'}
and
this.setState(produce(this.state, (draft) => { draft.name ='name'}
can someone explain me this?
In most cases both would act the same
produce((draft) => { draft.name ='name'})
returns a function that wraps argument with a Proxy and applies the mutation. setState
allows developer to pass a function that will be called with current state.
produce(this.state, (draft) => { draft.name ='name'})
returns the result of applying mutation to a Proxy of the first argument. setState
allows to pass "updated state" thus it will also work.
The first approach is preferable because of the following
setState
is the way to go.