How to use the same context multiple times in React.js in a nested way?

I have run into this situation a handful of times using React, but haven’t figured out a nice way to solve it, looking for a standard technique.

Basically, I want to use a React context more than once, in a nested way, but somehow be able to distinguish between the two.

Is it possible to do something like this?

import React, { useContext, useState } from 'react';

function MyApp() {
  const [selectedA, setSelectedA] = useState('');
  const [selectedB, setSelectedB] = useState('');
  return (
    <MyCustomContext.Provider
      value={{ id: 'a', selected: selectedA, setSelected: setSelectedA }}
    >
      <MyCustomContext.Provider
        value={{ id: 'b', selected: selectedB, setSelected: setSelectedB }}
      >
        <MyPage />
      </MyCustomContext.Provider>
    </MyCustomContext.Provider>
  );
}

function MyPage() {
  const { selected, setSelected } = useContext(MyCustomContext, 'a');

  return (
    <>
      <button onClick={() => setSelected('button')}>Select me</button>
      {selected === 'button' && <MyDrawer />}
    </>
  );
}

function MyDrawer() {
  const { selected, setSelected } = useContext(MyCustomContext, 'b');

  return (
    <div onClick={() => setSelected('this')}>
      {selected === 'this' ? 'I am selected' : null}
    </div>
  );
}

This would mean when the button is selected, the drawer opens. The open drawer then can be selected, to display a message. Meanwhile, the button is still staying selected. So you have two independent selection usages at once.

In reality, these two providers could be instantiated at arbitrary points in the React DOM tree, not necessarily so close together. But they would be nested inside each other, and one set of code might use selection to do one thing, while a different set of code uses selection context to do another thing. They both rely on the same functionality of the context/provider.

This is an overly simplified example, but you can imagine a more complex use case where you have selection managed for a datagrid in one example, and for the surrounding app form/ui or something like that. One is instantiated at the top-level, the other at the datagrid level, but the datagrid one is overriding the global one now. How can that be solved/fixed, without having to just duplicate the context module, creating two instances of the context constructor so useContext(Constructor) will be happy.

I understand that in some select use cases, you can separate the DOM tree so the two contexts aren’t nested (like using Portals). But let’s assume we can’t do that for one reason or another, and they MUST be nested. How can we solve this still?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật