1

just asking for some help with this basic implementation with hooks. I am currently working with Next.js and want to set a context in the _app.js file in order to make it available to the entire component tree.

import React from "react";

function MyApp({ Component, pageProps }) {
  const dataContext = React.createContext()
  return (
    <dataContext.Provider value = 'just some text'>
      <Component {...pageProps} />
    </dataContext.Provider>
  )
}
export default MyApp

Then in the index.js, add the component loginForm.js with the following code:

import React from "react";

function LoginForm (props) {

  const info = React.useContext(dataContext)
  const onSubmit = () => console.log(info)

return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <button type="submit">click here</button>
   </form>
)
}

now in the console, I receive an error that says "dataContext is not declared"

Apparently the context exists

  • Apparently, the context exists -

This is so simple that I don't even know how to tackle the issue, any help would be greatly appreciated.

1
  • You have to access the same instance of dataContext that's been declared in the _app. Try exporting it where you're declaring it export const DataContext = React.createContext(); then import and use it in the LoginForm component: import { DataContext } from '<path-to/pages/_app' & const info = React.useContext(DataContext). Have a read through the official docs. Commented Nov 8, 2021 at 18:03

3 Answers 3

1

What is dataContext in index.js?

 const info = React.useContext(dataContext)

I think you forgot to import the context

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

Comments

0

Move React.createContext() outside the component:

import React from "react";

const DataContext = React.createContext();

function MyApp({ Component, pageProps }) {
  return (
    <DataContext.Provider value="just some text">
      <Component { ...pageProps } />
    </DataContext.Provider>
  );
}

export default MyApp;

3 Comments

Thanks a lot for your help, unfortunately, the error persists.
@CristianDiaz Are you on the latest Next.js version? It might be related to this: github.com/vercel/next.js/issues/18090
Yes, I am working with Next v12.0.1, not sure if it is related to that issue, since in the description they receive an "undefined" value, in my case, it is basically that the context hook is not connecting to the context provider as if I were using a wrong name or if the context was set in another branch in the component tree, it does not appear to be the case. The most upsetting side is that this is not even something complex, it makes me feel so dumb.
0
let d = useMyContextHook()
const draw = d ? d.draw : null 

This fixed it for me

1 Comment

Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

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.