Giter VIP home page Giter VIP logo

react-multi-context's Introduction

React Multi Context

If you want to use React Context to separate the state from components, it can be challenging to manage contexts for the multiple data sources. If you store the data in one context, every update to the context will re-render all the components. This small utility library will help you create the nested contexts dynamically, and pull the data up in separate contexts.

Installation

npm install react-multiple-contexts

Use

You need to create a root React Context and pass it as a parameter to MultiContext. You will use this context to add inner Contexts, and to set their values.

import MultiContext from 'react-multiple-contexts';
const rootContext = React.createContext(null);
...

export default () => (
  <MultiContext rootContext={rootContext}>
    <App />
  </MultiContext>
);

To create the inner context, use the MultiContext function addInnerContext. It has only one parameter, the id of the inner context, and the optional second parameter if we want to pass the existing context:

    const rc = useContext(rootContext);
    ...
    Context1 = rc.addInnerContext(":a");

Why we need the id? The whole idea of this library is to separate the data from the components, and lift it up in separate contexts. When we need to update the context data, from inside our outside the component, we need the id. We do it with the root context function setInnerState:

 <button onClick={ev => rc.setInnerState(":b", x=>x?x+1:2)}>
    :b
 </button>

The first argument of this function is the context id. Another is the state function, that takes the existing context state and produces the new one.

Every adding of the new context to the provider causes the redraw. To avoid this, we can add the multiple inner contexts at once:

rc.addMultipleInnerContexts([":c", ":d"], [contextC, contextD]);

Example: See it on codesandbox

import React, { useContext, useEffect } from "react";
import MultiContext from "react-multiple-contexts";

const rootContext = React.createContext(null);
const context1 = React.createContext(null);
const context2 = React.createContext(null);

const B = () => {
  const value1 = useContext(context1);
  return (
    <div>
      B=
      {value1}
    </div>
  );
};

const C = () => {
  const value2 = useContext(context2);
  return (
    <div>
      C=
      {value2}
    </div>
  );
};

function App() {
  const rc = useContext(rootContext);
  useEffect(() => {
    rc.addMultipleInnerContexts([":b", ":c"], [context1, context2]);
  });
  return (
    <div className="App">
      <B />
      <C />
      <button
        onClick={() =>
          rc.setInnerState(":b", value => {
            console.log("current value " + value);
            return value ? value + 1 : 1;
          })
        }
      >
        Increase B
      </button>
      <button
        onClick={() => rc.setInnerState(":c", value => (value ? value + 2 : 1))}
      >
        Increase C
      </button>
    </div>
  );
}

export default () => (
  <MultiContext rootContext={rootContext}>
    <App />
  </MultiContext>
);

react-multi-context's People

Contributors

dependabot[bot] avatar dusanmiloradovic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.