All Projects → baptistelambert → react-combine-contexts

baptistelambert / react-combine-contexts

Licence: MIT license
🚀Use multiple React Contexts without pain.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-combine-contexts

react-patterns
react patterns examples
Stars: ✭ 39 (+69.57%)
Mutual labels:  context, hoc, render-props
React Fns
Browser API's turned into declarative React components and HoC's
Stars: ✭ 3,734 (+16134.78%)
Mutual labels:  hoc, render-props
React Wasm
Declarative WebAssembly instantiation for React
Stars: ✭ 349 (+1417.39%)
Mutual labels:  hoc, render-props
React Nprogress
⌛️ A React primitive for building slim progress bars.
Stars: ✭ 173 (+652.17%)
Mutual labels:  hoc, render-props
Flagged
Feature Flags for React made easy with hooks, HOC and Render Props
Stars: ✭ 97 (+321.74%)
Mutual labels:  hoc, render-props
react-app-loader
Production ready library for handling Microfrontends
Stars: ✭ 75 (+226.09%)
Mutual labels:  react-dom, hoc
react-zap
⚡ Zap props from one React component to another, using React new context API and your existing higher-order components ⚡
Stars: ✭ 17 (-26.09%)
Mutual labels:  context
xMenuTools
Extended context menu tools for Windows
Stars: ✭ 56 (+143.48%)
Mutual labels:  context
context
A proof of concept implementation of scoped context
Stars: ✭ 16 (-30.43%)
Mutual labels:  context
elcontext
Context-based actions for Emacs
Stars: ✭ 18 (-21.74%)
Mutual labels:  context
joincontext
Join contexts like never before!
Stars: ✭ 19 (-17.39%)
Mutual labels:  context
react-callbag-listener
👂 A React render-prop component that listens to values emitted by callbags
Stars: ✭ 21 (-8.7%)
Mutual labels:  render-props
context-mirror
Mirror of ConTeXt beta source code
Stars: ✭ 49 (+113.04%)
Mutual labels:  context
Hunch
Hunch provides functions like: All, First, Retry, Waterfall etc., that makes asynchronous flow control more intuitive.
Stars: ✭ 94 (+308.7%)
Mutual labels:  context
react-register-nodes
Register DOM Nodes in React
Stars: ✭ 32 (+39.13%)
Mutual labels:  react-dom
react-suspense-playground
Stock Market News app w/ React Suspense
Stars: ✭ 43 (+86.96%)
Mutual labels:  react-dom
nabbyl
A web app to get the color palette from your favorite albums.
Stars: ✭ 22 (-4.35%)
Mutual labels:  react-dom
next-password-protect
Password protect your Next.js projects.
Stars: ✭ 252 (+995.65%)
Mutual labels:  hoc
nestjs-cls
A continuation-local storage (async context) module compatible with NestJS's dependency injection.
Stars: ✭ 110 (+378.26%)
Mutual labels:  context
instagram-clone-frontend
📸 Um clone do instagram, onde você pode logar/registrar, criar novos posts, seguir outros usuários e ver os posts das pessoas que você segue.
Stars: ✭ 16 (-30.43%)
Mutual labels:  context

react-combine-contexts

Use multiple React Contexts without pain.

Installation

This package is compatible with react 16.3.0 and higher.

With npm:

npm install react-combine-contexts

Or with yarn:

yarn add react-combine-contexts

Usage

// src/index.js
import React, { Fragment } from 'react';
import ReactDOM from 'react-dom';

import { combineContexts } from 'react-combine-contexts';

import * as LocaleContext from './LocaleContext';
import * as ThemeContext from './ThemeContext';

// This will return an object that contains a Provider and a Consumer
const CombinedContext = combineContexts({
  locale: LocaleContext, // Associate each Context to a key
  theme: ThemeContext,
});

const App = () => (
  <Fragment>
    {/* Use the Context Provider like you would use it with any other Context */}
    <CombinedContext.Provider>
      <Fragment>
        {/* Use multiple contexts with a single Consumer */}
        <CombinedContext.Consumer>
          {({ locale, theme }) => (
            <div style={{ color: theme.color, fontFamily: theme.fontFamily }}>
              <span>Selected locale is {locale.selectedLocale}</span>
              <div>
                <button onClick={() => locale.setLocale('en')}>en</button>
                <button onClick={() => locale.setLocale('fr')}>fr</button>
              </div>
            </div>
          )}
        </CombinedContext.Consumer>
        {/* It is still possible to use a scoped Consumer */}
        <ThemeContext.Consumer>
          {theme => <div>The main color is {theme.color}.</div>}
        </ThemeContext.Consumer>
      </Fragment>
    </CombinedContext.Provider>
  </Fragment>
);

ReactDOM.render(<App />, document.getElementById('root'));

combineContexts

This function takes an object as a parameter and returns another object which contains a Provider and a Consumer.

For each key of the provided object you must provide a Context. The keys you indicated will be the ones used by the Consumer in the object passed as the parameter of its child function.

Inspiration

This package is inspired by react-adopt.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].