M
M
Maxim Akhmerov2020-02-15 12:41:47
React
Maxim Akhmerov, 2020-02-15 12:41:47

How react context works?

in the react dock about the context in the examples, everything is shown through the state, that if we want to change something in the context, then we need to send a callback to the state change through the context and only then the context will change, it turns out that without the root state we will not be able to in the context change data?
and a follow-up question about redax, as we know, redax works on contexts, it turns out there is most likely the same logic that when the store changes, we redraw the root element and all the others follow it, but there already coonect helps us not to redraw once again components whose props have not changed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Klein Maximus, 2020-02-15
@Maksum70

The context provider broadcasts some value to the child components.
It can be a state (reactive and mutable), it can be some kind of function (for example, to change the state), it can be just a native object in general (for example, some kind of application configuration that does not need to be changed) or just a constant (well, suddenly needed). It all depends on what you want to pass down the component tree.
Accordingly, if you need something that changes, is tracked and reactive, then yes, you need to transfer the state (the state of the provider) and there is no way without it.
It's very similar to Redux. But do not worry that for each sneeze the provider will redraw the entire tree. It's just that children should be passed to the provider through children. In this case, when the state changes, only the Consumers of this context (or those components that use useContext) will be re-rendered.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question