Skip to content
Advertisement

React native Context API react-navigation

React native: ^0.64.0,

@react-navigation/native-stack: ^6.1.0

@react-navigation/bottom-tabs: 6.0.5

@react-navigation/native: 6.0.2

I have a login screen. If the user passes the login screen the app leads the user to a the main part of the app. This is is the MainScreen with a bottom tab bar. The bottom tab bar gives the user the option to move between EditProfileScreen, SetupsScreen & TipsScreen.

I am setting up the react-navigation in the App.js:

JavaScript

I am trying to wrap Context API around my react-navigation elements in order to pass the data from the backend call (client/src/Login) to the (client/App.js) file. In order to be able to pass the data to all screens by the initial params. (https://reactnavigation.org/docs/params#initial-params)

My Context.js

JavaScript

My GlobalState.js

JavaScript

And my Login.js

JavaScript

But I can’t navigate to MainScreen because I get the following error:

TypeError: undefined is not an object (evaluating ‘this.context.tasks’)

So this.context.tasks is undefined in App.js. How can I make the context not undefined?

Thanks!

Advertisement

Answer

Since you have wrapped your entire navigation container with you can access the context data directly in component/screens

e.g. class component

JavaScript

functional component

JavaScript

functional component with hooks

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement