2023-03-24
2425
#react
Adebiyi Adedotun
20310
Mar 24, 2023 ⋅ 8 min read

React Context API: A deep dive with examples

Adebiyi Adedotun Caught in the web, breaking things and learning fast.

Recent posts:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 ⋅ 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 ⋅ 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 ⋅ 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 ⋅ 8 min read
View all posts

11 Replies to "React Context API: A deep dive with examples"

  1. What am I doing wrong? When I try, I get this error “Objects are not valid as a React child (found: object with keys…” Using react Version 17.x

    “`
    function UserProvider({children}) {
    const value = useState({
    name: ‘Guest’,
    email: false,
    is_logged_in: false,
    is_admin: false
    });

    return {children}
    }
    “`

  2. Great article! I’ve considered using context for form validation (i.e. validation errors from the server) so that all children (inputs) of a form can show validation errors without passing the errors array to each input. Redux (or similar) isn’t really appropriate here since there can be multiple forms on a page (at least we’ll need to identify each) and that validation errors are only relevant for descendants.

  3. Hi, In the Profile() method, how do I set the username? setUserDetails({username: “known-user”}) doesn’t seem to work.

  4. Traditionally, this is the case for all the reasons mentioned. Though you can try @webkrafters/react-observable-context on npm. It removes many of the redux and react context bottlenecks while making it easier to reuse your components.

  5. Also, instead of having two different contexts for passing down a value and setting the value, you can have this in one function and pass the value as an object containing the actual value and function which will update the value. For example, in your example:
    “`
    import React, { createContext, useState } from “react”;

    const UserContext = createContext(undefined);
    const UserDispatchContext = createContext(undefined);

    function UserProvider({ children }) {
    const [userDetails, setUserDetails] = useState({
    username: “John Doe”
    });

    return (

    {children}

    );
    }
    “`

    we can have this as:

    “`
    import React, { createContext, useState } from “react”;

    const UserContext = createContext(undefined);

    function UserProvider({ children }) {
    const [userDetails, setUserDetails] = useState({
    username: “John Doe”
    });

    return (

    {children}

    );
    }
    “`

    then in the component that uses this prop, obtain the values as:

    “`
    const {userDetails, setUserDetails} = useContext(UserContext);
    “`

Leave a Reply