Reputation: 873
I'm currently having react-native project v0.63.2 and using @react-navigation-5. Navigation from splash screen, login screen and tab screen is based from context.
appContext.js
import React from 'react';
const AppContext = React.createContext({IsLoading: true, IsLoggedIn: false});
export default AppContext;
navigationContainer.js
import AppContext from './appContext';
const StackApp = createStackNavigator();
export const StackNavigator = () => {
const [appState, setAppState] = React.useState({});
const state = { appState, setAppState };
React.useEffect(() => {
setAppState({ IsLoading: true, IsLoggedIn: false });
}, []);
return (
<AppContext.Provider value={state}>
{appState.IsLoading ? (
<SplashScreen />
)
: (
<NavigationContainer>
<StackApp.Navigator>
{
appState.IsLoggedIn
?
<>
<StackApp.Screen name='BottomTabScreen' component={BottomTabScreen} options={{ headerShown: false }} />
</>
:
<StackApp.Screen name='LoginScreen' component={NavigatorLogin} options={{ headerShown: false }} />
}
</StackApp.Navigator>
</NavigationContainer>
)}
</AppContext.Provider>
)
}
I recreate new login page with a class component. It can load all the previous way as functional component. But I unable to modify/update the context for IsLoggedIn: true
.
What I have tried:- initLogin.js
import AppContext from '../navigator/appContext';
const AppState = ({ newContext }) => {
const { setAppState } = React.useContext(AppContext);
console.log('setAppState:=> ' + JSON.stringify(setAppState));
console.log('newContext:=> ' + JSON.stringify(newContext));
setAppState(newContext);
}
export class initSignIn extends Component {
onPressLogin = () => {
AppState({ IsLoggedIn: true });
}
}
This will raise error hooks rules
Invalid hook call. Hooks can only be called inside of the body of a function component
I also tried use static context. No error but value undefined indicated the key IsLoggedIn
is not there.
Some of my reference:
I added snack minimal script. Might having error due to UI Kitten theme I use. I'm not familiar with snack Minimal Script
Upvotes: 0
Views: 622
Reputation: 16334
This would be a working example that uses context with a class component. Remember that you can use only one context when you are accessing it from a class.
Here I have created a button component which would update the context. As you can see i have function inside the context which would update the context which we pass the setAppState function from useState.
const AppContext = React.createContext({
appState: { IsLoading: true, IsLoggedIn: false },
setAppState: () => {},
});
export default function App() {
const [appState, setAppState] = React.useState({
IsLoading: false,
IsLoggedIn: false,
});
return (
<AppContext.Provider value={{ appState, setAppState }}>
<View style={styles.container}>
<Text style={styles.paragraph}>{JSON.stringify(appState)}</Text>
<Button />
</View>
</AppContext.Provider>
);
}
class Button extends React.PureComponent {
render() {
return (
<TouchableOpacity
onPress={() =>
this.context.setAppState({
IsLoading: !this.context.appState.IsLoading,
IsLoggedIn: true,
})
}>
<Text>Update</Text>
</TouchableOpacity>
);
}
}
Button.contextType = AppContext;
Url for snack https://snack.expo.io/@guruparan/contextwithclass
Upvotes: 1