1

I would like to see how I can create global contexts in NextJS and have global variables inside the app.

Currently I have AppContext.js as

import { createContext } from 'react'

const AppContext = createContext();

export default AppContext

_app.js


const [socketio, setSocketio] = useState(null);
const appContext = {
  socketio
};

return(
  <AppContext.Provider value={ appContext }>
    <Component {...pageProps} />
  </AppContext.Provider>
)

How can I access the appContext values from another component? Thank you!

1 Answer 1

1

You can use useContext(AppContext) to access the data from any component... Like:

const appContext = useContext(AppContext);

And access each value like

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

Comments

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.