H
H
h1tan2019-12-27 18:57:36
React
h1tan, 2019-12-27 18:57:36

Organization of handlers for useContext state?

Tell me how to organize a lot of handlers for the useState state through useContext ?
There is [canvas, setCanvas] = useState , which is connected to any component via useContext.
The important thing is that for canvas you need to create a lot of handlers like set, get, update, remove. All these handlers should be in different files and called anywhere. Also, just as importantly, handlers are likely to be able to call each other. Which caused me to close calls when creating custom hooks. For example: the setHandlers hook has a setConfig method. It sets a config, for example, and then must call another method from a different handler, such as getHistory. The most important thing is if you already understand what is at stake and how to deal with it. You need to take into account the history entity, for example, canvasHistory. Which any of the handlers should have access to. Those. the addObjectToCanvas method adds an object and should cause the canvas history to be updated. And here is just the problem in getting the last actual canvasHistory. In general, the problem is still the same) Who is able to help,

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
abberati, 2019-12-27
@abberati

https://gist.github.com/XaveScor/99431c573b53b8a0c...

T
twolegs, 2019-12-27
@twolegs

If there are so many actions that change the state, it will be easier to use the state manager, the same redux for example.
If you do not want to connect redux, then you can use useReducer instead of setState. In this case, it will be easier to store some kind of history of actions and keep track of the execution order of the handlers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question