Why do we useReducer (if useState)?

Preamble: for a Long time used react Redux typescript. Recently getting acquainted with hooks. Trying to build an application with global state passed through context.

Actually a question: why useReducer with all his boilerplate if it is possible to do the same with useState, just all the modification state of wrap in the same function-action?

I.e. instead of:
// index.js
const reducer (state, action) => {
 switch (action.type) {
 case 'actionType': {
 return {
 // change state here
}
}
}
}
const [state dispatch] = useReducer(reducer, {})

// actions.js
const someAction = () => {
 return {
 type: 'actionType'
}
}

// callback in someComponent.js
dispatch(someAction())


to use:
// index.js
const [state setState] = useState({});

// actions.js
const someAction = (setState, currentState) => {
setState({
 // change state here, in fact, as well as in redusere
})
}

// callback in someComponent.js
someAction(setState, currentState);


I do not see any benefit in the first approach, apart from the formalities, which inflate the code. Or am I still missing something?
April 3rd 20 at 18:50
1 answer
April 3rd 20 at 18:52
Solution
useReducer need to how to change state to encapsulate in one place — inside reducer. You described the approach loses in the "ponimayete" you're in actionCreator's smear logic storage.

And here is an interesting article about Stora on the hooks
https://gist.github.com/XaveScor/99431c573b53b8a0c...
thanks for the reply and the link. Digging into the subject, stopped on the choice of redaxo with hooks (useSelector). As I understand it, and performance won't lose, and the balance in the beauty/convenience acceptable for me. - Dewitt.Ritch commented on April 3rd 20 at 18:55

Find more questions by tags React