Using Provider in the Reactjs Context

Asked

Viewed 80 times

0

I’m trying to use the Reactjs Context but I can’t get the Provider to assign the value to the context variable. whenever I check the empty variable.

Context:

import React from 'react'
const AppContext = React.createContext({
  userData:{}
})
export default AppContext

Place where I pass the variable (there is value in it)

console.log("USUARIO", user)
    return (
        <AppContext.Provider value={{ userData: user }}>

Where I use Consumer:

 <AppContext.Consumer>
            {context => (
  • Hello Jason, in the log console before Return, is the user filled out? Where did you say you are checking the variable?

  • Yeah, I checked that out.

1 answer

0

I used this Context API only once. I did so:

In my App.js I instantiated the context like this

const INITIAL_STATE = {
  loading: false,
  login: localStorage.getItem("tork") ? true : false,
  error: ""
};

export const AppContext = React.createContext(INITIAL_STATE);

and to pass the context to the components I did so:

render() {
    return (
      <AppContext.Provider value={this.state}>
        <Routes signOut={() => this.signOut()} signIn={e => this.signIn(e)} />
      </AppContext.Provider>
    );
  }

I know it’s pretty much the same as yours, but see if it works that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.