All Projects → CharlesStover → react-multi-context

CharlesStover / react-multi-context

Licence: MIT license
Manage multiple React 16 contexts with a single component.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to react-multi-context

react-innertext
Returns the innerText of a React JSX object.
Stars: ✭ 37 (+94.74%)
Mutual labels:  babel, mocha, travis-ci, npm-package, travis, npm-module, babeljs, travisci
fetch-action-creator
Fetches using standardized, four-part asynchronous actions for redux-thunk.
Stars: ✭ 28 (+47.37%)
Mutual labels:  mocha, travis-ci, travis, npm-module, npmjs, travisci
Reactn
React, but with built-in global state management.
Stars: ✭ 1,906 (+9931.58%)
Mutual labels:  babel, travis-ci, travis, babeljs, travisci
react-testing-mocha-chai-enzyme
A solid test setup for React components with Mocha, Chai, Sinon, Enzyme in a Webpack/Babel application.
Stars: ✭ 48 (+152.63%)
Mutual labels:  mocha, enzyme, travis-ci, travis
react-native-web-view
An implementation of React Native's WebView that allows for postMessage on iOS devices.
Stars: ✭ 13 (-31.58%)
Mutual labels:  travis-ci, babeljs, npmjs, travisci
js-stack-from-scratch
🌺 Russian translation of "JavaScript Stack from Scratch" from the React-Theming developers https://github.com/sm-react/react-theming
Stars: ✭ 394 (+1973.68%)
Mutual labels:  mocha, enzyme, npm-package, npm-module
Webpack React Boilerplate
Minimal React 16 and Webpack 4 boilerplate with babel 7, using the new webpack-dev-server, react-hot-loader, CSS-Modules
Stars: ✭ 358 (+1784.21%)
Mutual labels:  babel, enzyme, jest
Express React Boilerplate
🚀🚀🚀 This is a tool that helps programmers create Express & React projects easily base on react-cool-starter.
Stars: ✭ 32 (+68.42%)
Mutual labels:  babel, jest, npm-package
React Pages Boilerplate
Deliver react + react-router application to gh-pages
Stars: ✭ 134 (+605.26%)
Mutual labels:  babel, enzyme, jest
Lighthouse Badges
🚦Generate badges (shields.io) based on Lighthouse performance.
Stars: ✭ 150 (+689.47%)
Mutual labels:  babel, jest, travis
2019 12
🎟 급증하는 트래픽에도 안정적인 예약 서비스, Atomic Pattern을 적용한 재사용 가능한 컴포넌트, 실용적인 Testing을 주제로 하는 이벤트 서비스
Stars: ✭ 169 (+789.47%)
Mutual labels:  enzyme, jest, travis-ci
Iceberg
Front-End Boilerplate built with React + Babel + Webpack + SASS
Stars: ✭ 144 (+657.89%)
Mutual labels:  babel, jest, travis-ci
React Redux Universal Boilerplate
An Universal ReactJS/Redux Boilerplate
Stars: ✭ 165 (+768.42%)
Mutual labels:  babel, mocha, enzyme
babel-plugin-source-map-support
A Babel plugin which automatically makes stack traces source-map aware
Stars: ✭ 41 (+115.79%)
Mutual labels:  babel, npm-package, npm-module
Forge Node App
🛠📦🎉 Generate Node.js boilerplate with optional libraries & tools
Stars: ✭ 90 (+373.68%)
Mutual labels:  babel, npm-package, npm-module
Enzyme
JavaScript Testing utilities for React
Stars: ✭ 19,781 (+104010.53%)
Mutual labels:  mocha, enzyme, jest
React
Extremely simple boilerplate, easiest you can find, for React application including all the necessary tools: Flow | React 16 | redux | babel 6 | webpack 3 | css-modules | jest | enzyme | express + optional: sass/scss
Stars: ✭ 244 (+1184.21%)
Mutual labels:  babel, enzyme, jest
Generator Rn Toolbox
The React Native Generator to bootstrap your apps
Stars: ✭ 1,155 (+5978.95%)
Mutual labels:  jest, travis-ci, travis
personal-blog
✍️ 个人技术博客
Stars: ✭ 79 (+315.79%)
Mutual labels:  enzyme, jest, travis-ci
Babel Plugin Tester
Utilities for testing babel plugins
Stars: ✭ 228 (+1100%)
Mutual labels:  babel, mocha, jest

React Multi-Context Tweet

Manage multiple contexts with a single React component.

version minified size minzipped size downloads build

Install

  • npm install react-multi-context --save or
  • yarn add react-multi-context

Test

  • npm run test or
  • yarn test

Use

import createMultiContext from 'react-multi-context';
export const MyMultiContext = createMultiContext();

Create the context by importing and executing createMultiContext wherever you want to create context. Then, import that multi-context instance as needed.

Use the set prop to set a context's value. Changing the value on a key-value pair in a context will cause all getters for that key to re-render.

Use the get prop to get a context's value. Using this prop will execute the children render prop by passing the corresponding values of the context as the parameters.

Example

// Parent.js
import createMultiContext from 'react-multi-context';

// Context to provide to children.
export const ParentContext = createMultiContext();

export default class Parent extends React.Component {
  render() {
    return (
      <ParentContext
        set={{
          cost: 10,
          project: {
            name: 'React Multi-Context',
            version: 1.0
          },
          user: 'Charles'
        }}
      >
        <Child />
      </ParentContext>
    );
  }
}
// Child.js
// Get the context provided from the parent
import { ParentContext } from './Parent';

export default class Child extends React.Component {
  render() {
    return (
      <ParentContext get={[ 'project', 'user' ]}>
        {(project, user) =>

          /* This is a demo of React Multi-Context v1.0 by Charles! */
          <p>This is a demo of {project.name} v{project.version} by {user}!</p>
        }
      </ParentContext>
    );
  }
}

Example (Shorter)

// Parent - writes A and B
const Parent = () =>
  <MultiContextInstance set={{ a: 1, b: 2 }}>
    <Child1 />
    <Child2 />
    <Child3 />
  </MultiContextInstance>;
// Child1 - reads A
// Note: Each value is its own context, which is what makes this MULTI-context.
const Child1 = () =>
  <MultiContextInstance get={[ 'a' ]}>
    {(a) => `The value of A is ${a}!`}
  </MultiContextInstance>;
// Child2 - reads A and B
// Note: Reading multiple values will trigger a re-render if any one read value changes.
const Child2 = () =>
  <MultiContextInstance get={[ 'a', 'b' ]}>
    {(a, b) => `The value of A+B is ${a + b}!`}
  </MultiContextInstance>;
// Child3 - reads B and A
// Note: The order of the get prop corresponds to the order of the function parameters.
const Child3 = () =>
  <MultiContextInstance get={[ 'b', 'a' ]}>
    {(b, a) => `The value of A+B is ${a + b}!`}
  </MultiContextInstance>;

Default Values

You may pass an object of default values for the contexts as a parameter to createMultiContext or via the default prop.

const MyMultiContext = createMultiContext({ a: 0, b: 0 });

or

<MyMultiContext
  default={{ a: 0, b: 0 }}
  set={{ a: 1 }}
>
  <MyMultiContext get={[ 'b' ]}>
    {b => 'I predict that B equals zero: ' + b}
  </MyMultiContext>
</MyMultiContext>

You do not need to do both.

MultiContext.with

MultiContextInstance.with(...multiContextKeys)(Component) will bind the multiContextKeys of MultiContextInstance to the props of Component.

import React from 'react';
import { SomeMultiContext } from './some-component';

class MyComponent extends React.PureComponent {
  render() {
    return <div children={'My name is ' + this.props.name + ', and I am ' + this.props.age + '!'} />;
  }
}

// Binds the MultiContext's `name` property to MyComponent's `name` prop.
export default SomeMultiContext.with('name', 'age')(MyComponent);

Sponsor 💗

If you are a fan of this project, you may become a sponsor via GitHub's Sponsors Program.

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].