KennyUIowa KennyUIowa - 3 months ago 35
React JSX Question

Does React Native have global scope? If not, how can I mimic it?

I haven't been able to find anything like

$rootScope


for React Native and I want to share a couple variables between different Views.

Answer

Storing variables in the global scope is a bit of an anti-pattern in React. React is intended to be used with a one-way data flow, meaning data flows down from components to their children. This is achieved through the passing of props.

In order to share a value between multiple views, you would either need to store the data on a shared parent component and pass it down as a prop OR store it in a separate entity that is responsible for data management. In ReactJS this is achieved through libraries like Flux, but I'm not sure about the options for react-native.