I am getting start to study Context API of the React Native. I understand that the Context API is to solve the problem to send a lot of props in the parameters. It seems to me as a global variable. In this case, to use a static variable of a class in JS don't fix the problem of a variable global? Why use Context API when we can use a static variable? What are better in Context API? Are others API that use Context API in React Native as pre-requisite?
Asked
Active
Viewed 498 times
1
-
When you update a static variable React doesn't know about it, so it doesn't re-render any of the components that are relying on it. If you put that variable into react state then you can pass it through your tree via context which will trigger re-renders – azium Jul 08 '21 at 01:01
1 Answers
1
In my experience you can do exactly as you're describing...
You'd set a static property
App.instance = this
in App's constructor.Your
App
class has static methods which can accessApp.instance.state
andApp.instance.setState()
.
I'm curious why this approach isn't mentioned anywhere. Possibly because you can't use static properties in functional components, so it's a bit unfashionable. And it feels like it goes against react's component tree structure.
There is also the general feeling that statics are evil.

chichilatte
- 1,697
- 19
- 21