36
loading...
This website collects cookies to deliver better user experience
Context provides a way to pass data through the component tree without having to pass props down manually at every level.
useContext.js
file, which is where the context instance and the variables to be used will be created.useContext.js
, create the context object by importing and using createContext
import React, { createContext, useState } from "react";
export const MyContext = createContext();
export const UserProvider = ({ children }) => {
const [name, setName] = useState("");
const [lastName, setLastName] = useState("");
return (
<MyContext.Provider
value={{
name,
setName,
lastName,
setLastName
}}
>
{children}
</MyContext.Provider>
);
};
MyContext
that will be used in the child components. useState
to maintain the state of the Name
and lastName
variables, with their corresponding methods.value
. The provider serves to provide context to child components.index.js
file is imported from the UserProvider
context file useContext.js
. So we're going to wrap the <App/>
with the UserProvider
like this:import ReactDOM from "react-dom";
import { UserProvider } from './useContext';
import App from "./App";
const rootElement = document.getElementById("root");
ReactDOM.render(
<UserProvider>
<App />
</UserProvider>,
rootElement
);
value
in our context file can be accessed in other components.ComponentName.js
and ComponentLastName.js
. In both files it is necessary to import the MyContext
from our context file and the useContext
hook that will be used to set the context that we will use to access the available data. Staying like this:import React, { useContext } from "react";
import { MyContext } from "./useContext";
const Name = () => {
const user = useContext(MyContext);
return (
<div>
<h2>
<strong>Name</strong>: {user.name}
</h2>
</div>
);
};
export default Name;
import React, { useContext } from "react";
import { MyContext } from "./useContext";
const LastName = () => {
const user = useContext(MyContext);
return (
<div>
<h2>
<strong>Last Name</strong>: {user.lastName}
</h2>
</div>
);
};
export default LastName;
const user = useContext(MyContext);
user
const will be responsible so that we can access the global variables of our context.App.js
file, we import the MyContext
and using the useContext
hook we will consume the data from our context. With the setName
and setLastName
methods retrieved from the context, we call onChange
on the respective inputs so that the data is updated with each character typed by the user. Staying like this:import React, { useContext } from "react";
import { MyContext } from "./useContext";
import Name from "./ComponentName";
import LastName from "./ComponentLastName";
import "./styles.css";
export default function App() {
const user = useContext(MyContext);
return (
<div className="App">
<div>
<div>
<label className="label">Name: </label>
<input
onChange={(event) =>
user.setName(event.target.value)} />
</div>
<div>
<label>Last Name: </label>
<input
onChange={(event) =>
user.setLastName(event.target.value)}
/>
</div>
</div>
<Name />
<LastName />
</div>
);
}
ComponentName.js
and ComponentName.js
.