Issue
So I started learning to react native and I faced the challenge of central data storage. Past in react I used contexts. Is react native supporting it? I haven't learned Redux, but in this case it's not a problem for me. I've tried to do this in the classic react way
import React, {useState, createContext} from 'react';
export const LoginTokenContext = createContext();
export const LoginProvider = props => {
const [loginToken, setLoginToken] = useState( localStorage.getItem('loginToken') );
const [loginName, setLoginName] = useState( localStorage.getItem('loginName') );
return(
<LoginContext.Provider value={ {loginToken, setLoginToken, loginName, setLoginName} }>
{props.children}
</LoginContext.Provider>
);
} ;
and it didn't work (may my bad implementation). I've done also some research but couldn't find any useful information.
Solution
You have defined the context to be LoginTokenContext
and you have used is as LoginContext.Provider
. There is a typo here.
Change to:
<LoginTokenContext.Provider value={ {loginToken, setLoginToken, loginName, setLoginName} }>
{props.children}
</LoginTokenContext.Provider>
Answered By - Jagrati
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.