Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global state in React Native

Tags:

I am developing a React Native application.

I want to save the user id of the person who is logged in and then check if the user is logged in in every single component.

So what I am looking for is something like cookies, sessions or global states.

I have read that I should use Redux, but this seems to be overly complicated and it is very difficult to make it work with react-navigation. It forces me to define actions and reducers for almost everything although the only thing I want is to be able to access a single global state/variable in all components.

Are there any alternatives or should I really re-structure my entire app to use Redux?

like image 526
Jamgreen Avatar asked May 28 '17 12:05

Jamgreen


People also ask

What is global state in react native?

Role of the global state. In React, originally, the state is held and modified within the same React component . In most applications, different components may need to access and update the same state. This is achieved by introducing the global states in your app.

How do you use state globally in React?

The idea of context is exactly that - for you to be able to share updateable state to the descendants without having to pass it from component to component. const UserContext = React. createContext(null); const App = () => { const [user, setUser] = useState(null); return ( <Router> <div className="app"> <UserContext.

Why do we need a global state?

Global state is necessary when we want to get and update data anywhere in our app, or in multiple components at least. A common example of global state is authenticated user state. If a user is logged into our app, it is necessary to get and change their data throughout our application.


2 Answers

I usually create a global.js containing:

module.exports = {    screen1: null, }; 

And get the value of the state on the screen

import GLOBAL from './global.js'  constructor() {      GLOBAL.screen1 = this;  } 

Now you can use it anywhere like so:

GLOBAL.screen1.setState({     var: value }); 
like image 172
Raphael Estrada Avatar answered Sep 20 '22 20:09

Raphael Estrada


Update since React 16.8.0 (February 6, 2019) introduce Hooks.

it is not mandatory to use external library like Mobx or Redux. (Before Hook was introduce I used both of this state management solutions)

you can create global state just with 10 line Source

import React, {createContext, useContext, useReducer} from 'react'; export const StateContext = createContext(); export const StateProvider = ({reducer, initialState, children}) =>(   <StateContext.Provider value={useReducer(reducer, initialState)}>     {children}   </StateContext.Provider> ); export const useStateValue = () => useContext(StateContext); 

extend your app with global state:

import { StateProvider } from '../state';  const App = () => {   const initialState = {     theme: { primary: 'green' }   };    const reducer = (state, action) => {     switch (action.type) {       case 'changeTheme':         return {           ...state,           theme: action.newTheme         };        default:         return state;     }   };     return (     <StateProvider initialState={initialState} reducer={reducer}>         // App content ...     </StateProvider>   ); } 

For details explanation I recommend to read this wonderful medium

like image 37
Chotala Paresh Avatar answered Sep 17 '22 20:09

Chotala Paresh