27
loading...
This website collects cookies to deliver better user experience
createContext()
method shipped with React. This context will be the global state available for use across the app. Well, at least one them because your React app can contain infinite number of contexts.// context.jsx
// Import createContext() method from React:
import { createContext } from 'react'
// Create new context:
export const newContext = createContext()
undefined
. Don't worry. This doesn't mean this context will be empty forever. It will be empty just for now when you create it. Later, in the next step, you will add values to it. Also notice that we are exporting the context.return
statement. Here, you will use the context for the first time. The context you've previously created also contains a provider component your new provider will render. You can access this provider component using object dot notation (newContext.Provider
).// context.jsx
// Import createContext() method from React:
import { createContext } from 'react'
// Create new context:
export const newContext = createContext()
// Create new provider component:
export const NewProvider = (props) => {
return (
{/* Render Provider provided by previously created context: */}
<newContext.Provider>
{/* Render Provider's children: */}
{props.children}
</newContext.Provider>
)
}
// index.jsx
// Import React and React-dom:
import { StrictMode } from 'react'
import ReactDOM from 'react-dom'
// Import the NewProvider component:
import { NewProvider } from './context'
// Import app component:
import App from './App'
// Create the main component:
const rootElement = document.getElementById('root')
ReactDOM.render(
<StrictMode>
{/* Use the NewProvider to wrap the whole app: */}
<NewProvider>
{/* The app component rendering all other components: */}
<App />
</NewProvider>
</StrictMode>,
rootElement
)
value
attribute to the myContext.Provider
component returned by the NewProvider
component. The value of this attribute can be anything from a primitive data type to an object. If you want to share a single value, the first will be sufficient.// context.jsx
// Import createContext() method and useState hook from React:
import { createContext, useState } from 'react'
// Create new context:
export const newContext = createContext()
// Create new provider component:
export const NewProvider = (props) => {
// Create local state:
const [state, setState] = useState('')
// Prepare values to share:
const val = {
state, // The state itself
setState // The state update function
}
return (
{/* Set "val" as the value for "value" attribute: */}
<newContext.Provider value={value}>
{props.children}
</newContext.Provider>
)
}
value
attribute. You can now access the state and setState function exposed via the provider anywhere in the app.createContext()
method. When you combine these two you will have immediate access to state
and setState
you created in NewProvider
component.App
component. You saw this component in the index.jsx
file as the direct child of the provider (Creating the context provider section). This component will be simple. It will contain two components: heading showing welcome message and current value of state
and input to update the state
via setState
.state
and setState
, from the newContext
context. Remember that this context is provided by the NewProvider
component. You will get those values by calling the React useContext hook and passing the newContext
context as an argument.// Import useContext hook from React:
import { useContext } from 'react'
// Import newContext context:
import { newContext } from './context'
// Create the App component:
export default function App() {
// Access the state and setState values in newContext:
const { state, setState } = useContext(newContext)
return (
<div>
{/* Display the value of "state" */}
<h1>Hello {state}</h1>
<h2>Change name:</h2>
{/*
Use "setState" update function to update the current value
of "state" with the current value of input:
*/}
<input type="text" onChange={(e) => setState(e.target.value)} />
</div>
)
}
// email-context.jsx
// Import createContext() method from React:
import { createContext, useState } from 'react'
// Create new context:
export const emailContext = createContext()
// Create new email provider component:
export const EmailProvider = (props) => {
// Create local state for email:
const [email, setEmail] = useState('')
// Prepare values for sharing:
const val = {
email,
setEmail,
}
// Render emailContext.Provider exposing "val" variable:
return (
<emailContext.Provider value={val}>
{/* Render children components: */}
{props.children}
</emailContext.Provider>
)
}
App
to the root element. When you have multiple providers their order doesn't really matter. Important thing that the app, or some component where you want to use data from those providers, is wrapped with those providers.import { StrictMode } from 'react'
import ReactDOM from 'react-dom'
import { NewProvider } from './context'
// Import new email provider:
import { EmailProvider } from './email-context'
import App from './App'
const rootElement = document.getElementById('root')
ReactDOM.render(
<StrictMode>
{/* Add email provider as another wrapper of the App component: */}
<EmailProvider>
<NewProvider>
<App />
</NewProvider>
</EmailProvider>
</StrictMode>,
rootElement
)
emailContext
to access the email
and setEmail
anywhere in the app.import { useContext } from 'react'
import { newContext } from './context'
// Import new email context:
import { emailContext } from './email-context'
export default function App() {
const { state, setState } = useContext(newContext)
// Access the email and setEmail values in emailContext:
const { email, setEmail } = useContext(emailContext)
return (
<div>
{/* Render the value of "email": */}
<h1>
Hello {state}, {email}
</h1>
<h2>Change name:</h2>
<input type="text" onChange={(e) => setState(e.target.value)} />
<h2>Change email:</h2>
{/*
Allow to to update the current value of "email"
via the "setEmail" update function and text input:
*/}
<input type="text" onChange={(e) => setEmail(e.target.value)} />
</div>
)
}