Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 5, 2021 07:26 am GMT

Understanding useContext in React

useContext

In React, context is more like a global variable that can be used across all components in an application. An example of when to use the context hook is to set the preferred theme or to store the currently signed-in user.

You should use the context hook only when you need some data to be accessible by many components.

working with useContext

To understand useContext better we'll be creating a context that stores a user's details and we'll be showing some things to the user if their details are stored in the context.

First things

You should have a react app created already and install react-router-dom from npm (just for this example, you don't need react-router-dom for useContext). Now create a file in your source folder and name it userDetails.jsx this is the file that will be creating the context. Then do this.

import { useState, createContext } from 'react';const UserContext = createContext(); const UserProvider = (props) => {    const [username, setUsername] = useState('');// the state that we'll be storing the username into    return (        <UserContext.Provider            value={{username, setUsername}}        >            {props.children}        </UserContext.Provider>    );}export { UserContext, UserProvider };
Enter fullscreen mode Exit fullscreen mode

In the code above, we just created a context called UserContext using react's createContext(), create context will tell react that we want to create a global variable. Then we created a component that contains the state we want to access globally. You'll notice that we're using a provider from the UserContext. UserContext.Provider The provider is a method from useContext that we can warp all the other components in like we're about to do in our App component.

In the App.jsx file import the UserProvider that was exported.

import { BrowserRouter, Switch, Route } from "react-router-dom";import { UserProvider } from './userDetails';const App = () =>  {  return (  <UserProvider>      <BrowserRouter>        <Switch>          <Route path="/" exact component={SetUserDetails} />          <Route             path="/user"             exact             component={FetchUserDetails} />        </Switch>      </BrowserRouter>    </UserProvider> )}export default App;
Enter fullscreen mode Exit fullscreen mode

In the code above we're wrapping the provider into our other components. Let's create the components in the Route and use the context inside them.

Create a file and name it SetUserDetails.jsx and paste this in the file

import React, { useState, useContext } from "react";import { useHistory } from "react-router-dom";import { UserContext } from "./userDetails";const SetUserDetails = () => {  const [name, setName] = useState("");  const history = useHistory();  const { setUsername } = useContext(UserContext);  const handleSetName = () => {    setUsername(name);    history.push("/user");  };  return (    <>      <input           value={name}           onChange={(e) => setName(e.target.value)} />      <button onClick={handleSetName}>Set Name </button>    </>  );};export default SetUserDetails;
Enter fullscreen mode Exit fullscreen mode

In the code above we created a component that accepts a username and stores it into our context. You'll notice the use of the useContext hook. We're using the hook to get the context we created earlier, in this case, we're getting setUsername. Once the user clicks on the button it will assign the name in this local state to the context.

Next, let's get the context. Create a file and name it FetchUserDetails.jsx (this is the other file in the route)

Then paste this into the file.

import React, { useContext } from "react";import { UserContext } from "./userDetails";const FetchUserDetails = () => {  const { username } = useContext(UserContext);  return <>{username ? `Hello ${username}` : `Hello User`}</>;};export default FetchUserDetails;
Enter fullscreen mode Exit fullscreen mode

Here, we're getting the username state and checking for a value in the state. if it is empty we'll display 'Hello User', and if not we'll display the username.

Try running the code and testing it.

You'll notice that the username is available in the other component. Thanks to useContext hook.

Now, try creating a context on your own and also try persisting the context.

Conclusion

This is just a basic usage for useContext there's more you can do with context, like saving a user's preferred theme or some other preferences and persisting it in your local storage.

Thank you for reading.
In my next post, I'd be looking at the useMemo hook.
If you enjoyed this post please like and share. If you have questions please feel free to drop them in the comments section. Keep coding and doing amazing things.


Original Link: https://dev.to/ilizette/understanding-usecontext-in-react-26gf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To