1

I have a simple react app in which i have to use useContext.

(btw im using vite + react)

here is my code for Context.jsx

import React, {useContext} from 'react';

const emailContext = React.createContext();

export const useEmail = () => useContext(emailContext);

export const emailProvider = ({children}) => {
  const currentUser = "None";

  const value = {
    currentUser
  }

  return(
    <emailContext.Provider value={value}>
      {children}
      </emailContext.Provider>
  )
}

and heres how i am using the context

import "./styles.css";
import { useEmail } from "./Context/Context"

export default function App() {

  const {currentUser} = useEmail();

  return (
    <div className="App">
      <h1>Hello CodeSandbox {currentUser}</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}

I am sure why I am getting error in this code.

some of the errors that I am getting

  • _useEmail is undefined (latest)
  • currentUser user is undefined

thing i have tried

  • Initialized createContext with some initial value (only intial value is visible).
  • using useContext() directy in the App.js (useContext(emailContext) return undefined)
  • instead of {children} used <children/>.
  • used useState instead of const currentUser in emailProvider

I am getting same problem even when I use typescript.

but none of the above helped.

2
  • I have read some implemetation on the same for global state management with context api you can check it out. Provides a detailed look on the global state. Here is the link Similar implementation Commented Nov 8, 2021 at 4:10
  • 1
    I think it's a vite issue. Commented Mar 2, 2022 at 11:37

3 Answers 3

0

You should wrapping app with <emailProvider></emailProvider> to using data in value={value}. Now it gets undefined from const emailContext = React.createContext();

Sign up to request clarification or add additional context in comments.

2 Comments

export default function App() { var value = useEmail(); console.log(value) return ( <div className="App"> <EmailProvider> <h1>Hello CodeSandbox</h1> <h2>Start editing to see some magic happen!</h2> </EmailProvider> </div> ); } Even after this the code is not working, If place value.currentUser in the inside the EmailProvider component it will throw an error
var value = useEmail(); should be inside a component that's wrapped with the emailProvider
0

Below code may help you analyse the flow , also check link for more details https://medium.com/technofunnel/usecontext-in-react-hooks-aa9a60b8a461

use useContext in receiving end

 import React, { useState } from "react";

var userDetailContext = React.createContext(null);

export default function UserDetailsComponent() {
  var [userDetails] = useState({
    name: "Mayank",
    age: 30
  });

  return (
    <userDetailContext.Provider value={userDetails}>
      <h1>This is the Parent Component</h1>
      <hr />
      <ChildComponent userDetails={userDetails} />
    </userDetailContext.Provider>
  );
}

function ChildComponent(props) {
  return (
    <div>
      <h2>This is Child Component</h2>
      <hr />
      <SubChildComponent />
    </div>
  );
}

function SubChildComponent(props) {
  var contextData = React.useContext(userDetailContext);
  return (
    <div>
      <h3>This is Sub Child Component</h3>
      <h4>User Name: {contextData.name}</h4>
      <h4>User Age: {contextData.age}</h4>
    </div>
  );
}

1 Comment

On same file it working, but if the files are different it is not working. Also, I have to pass components dynamically. For the medium page, I cannot view it because it requires subscription.
0

in index.js/main.js file

<emailProvider/> <App/> <emailProvider>

this helped me

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.