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:

Radix Ui Adoption Guide Overview Examples And Alternatives

Radix UI adoption guide: Overview, examples, and alternatives

Radix UI is quickly rising in popularity and has become an excellent go-to solution for building modern design systems and websites.

Nefe Emadamerho-Atori
Apr 25, 2024 ⋅ 11 min read
Understanding The Css Revert Layer Keyword, Part Of Css Cascade Layers

Understanding the CSS revert-layer keyword

In this article, we’ll explore CSS cascade layers — and, specifically, the revert-layer keyword — to help you refine your styling strategy.

Chimezie Innocent
Apr 24, 2024 ⋅ 6 min read
Exploring Nushell, A Rust Powered, Cross Platform Shell

Exploring Nushell, a Rust-powered, cross-platform shell

Nushell is a modern, performant, extensible shell built with Rust. Explore its pros, cons, and how to install and get started with it.

Oduah Chigozie
Apr 23, 2024 ⋅ 6 min read
Exploring Zed, A Newly Open Source Code Editor Written In Rust

Exploring Zed, an open source code editor written in Rust

The Zed code editor sets itself apart with its lightning-fast performance and cutting-edge collaborative features.

Nefe Emadamerho-Atori
Apr 22, 2024 ⋅ 7 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