# react-context-bridge

> A useful util to pass context between multiple renderers

Latest version **0.0.5** (published 2022-09-02) · ISC license · 0 weekly downloads

## Install

```sh
npm install react-context-bridge
pnpm add react-context-bridge
yarn add react-context-bridge
bun add react-context-bridge
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2022-09-02 |
| First published | 2022-08-30 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 17.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | xingyu.hu |
| Maintainers | xingyu.hu |
| Keywords | react, reactjs, context, useContext |

## Links

- npm: https://www.npmjs.com/package/react-context-bridge
- npm.io page: https://npm.io/package/react-context-bridge

## Dependencies (1)

- [@types/react](https://npm.io/package/@types/react.md) ^18.0.18

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2022-09-02
- 0.0.4 — 2022-09-01
- 0.0.3 — 2022-08-31
- 0.0.2 — 2022-08-30
- 0.0.1 — 2022-08-30

## README

# useContextBridge
```tsx
const Context1 = createContext()
const Context2 = createContext()
const contexts = [Context1, Context2]

function Test(){
    const { ContextListeners, ContextProviders } = useContextBridge(contexts)

    useEffect(()=>{
        // in others renderer, so you can share context
        createRoot(document.createElement('div')).render(
            <ContextProviders>
              <YourComponent />
            </ContextProviders>
        )
    },[])

    return (
        // insert it anywhere
        <ContextListeners />
        <div>{/* ... */}</div>
    )
}
```

# createContext

```tsx
import React from "react";
import { createRoot } from "react-dom/client";
import { createContext } from "react-context-bridge";

const Context = createContext<any>(false);

const Provider: React.FC<React.PropsWithChildren<{}>> = ({ children }) => {
  const [toggle, setToggle] = React.useState(false);

  return (
    <Context.Provider value={{ toggle, setToggle }}>
      {children}
    </Context.Provider>
  );
};

const ToggleButton = () => {
  const { toggle, setToggle } = Context.useContext();
  return (
    <button onClick={() => setToggle(!toggle)}>{toggle ? "on" : "off"}</button>
  );
};

const Page = () => {
  const { toggle } = Context.useContext();
  return toggle ? <div>1</div> : <div>0</div>;
};

const arr = [...new Array(10).keys()];
const App: React.FC = () => {
  React.useLayoutEffect(() => {
    arr.forEach((_, idx) => {
      const ele = document.getElementById("hook" + idx);
      if (ele) {
        createRoot(ele).render(
          <Context.Injector>
            {idx % 2 === 0 ? <Page /> : <ToggleButton />}
          </Context.Injector>
        );
      }
    });
  }, []);

  return (
    <Provider>
      <Page />
      {arr.map((_, idx) => (
        <div id={"hook" + idx} key={idx}></div>
      ))}
    </Provider>
  );
};

export default App;
```

---
_Source: https://npm.io/package/react-context-bridge · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
