Giter Club home page Giter Club logo

react-context-composer's Introduction

DEPRECATED

With the advent of React's new hooks feature, this package has little value. Instead of rendering your contexts, you can use the react hook to access it in the function definition. An example:

import { useContext } from 'react';
import { ContextA, ContextB, ContextC } from './contexts';

function Component() {
  const a = useContext(ContextA);
  const b = useContext(ContextB);
  const c = useContext(ContextC);
  
  return (<...>);
}

react-context-composer

React 16.3 shipped a new Context API. The API encourages composition. This utility component helps keep your code clean when your component will be rendering multiple Context Providers and Consumers.

Usage

import React, { Component } from 'react'
import ContextComposer from 'react-context-composer';
import createReactContext, { type Context } from 'create-react-context';

type Theme = 'light' | 'dark';
type Language = 'en';

// Pass a default theme to ensure type correctness
const ThemeContext: Context<Theme> = createReactContext('light');
const LanguageContext: Context<Language> = createReactContext('en');

class ThemeToggler extends React.Component<
  { children: Node },
  { theme: Theme, language: Language }
> {
  state = { theme: 'light', language: 'en' };
  render() {
    return (
      // Pass the current context value to the Provider's `value` prop.
      // Changes are detected using strict comparison (Object.is)
      <ContextComposer contexts={[
        <ThemeContext.Provider value={this.state.theme} />
        <LanguageContext.Provider value={this.state.language} />
      ]}>
        <button
          onClick={() => {
            this.setState(state => ({
              theme: state.theme === 'light' ? 'dark' : 'light'
            }));
          }}
        >
          Toggle theme
        </button>
        {this.props.children}
      </ContextComposer>
    );
  }
}

function Title({children}) {
  return (
    <ContextComposer contexts={[ThemeContext, LanguageContext]}>
      {(theme, language) => (
        <h1 style={{ color: theme === 'light' ? '#000' : '#fff' }}>
          {language}: {children}
        </h1>
      )}
    </ContextComposer>
  );
}

render(
  <ThemeToggler>
    <Title>Hi!</Title>
  </ThemeToggler>,
  domNode
);

License

MIT © Blaine Kasten

react-context-composer's People

Contributors

blainekasten avatar diegohaz avatar jamesplease avatar justingreenberg avatar matthamil avatar motiz88 avatar samouss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-context-composer's Issues

Contexts array is mutated during consumer composition

For consumer composition, ContextComposer calls Array.prototype.reverse on the contexts prop, resulting in its mutation.

Mutating props inside components is not generally safe or expected behaviour. Moreover, if the call site reuses the same array instance across renders, we will end up reversing the contexts and forth which is most certainly not what the supplied children function is expecting.

Composing empty list of consumers shouldn't crash

This is fairly minor but I intend to fix it along with #8.

When rendering the following snippet, render should be called with no arguments, as a logical extension of the behaviour with one or more contexts.. Currently it just crashes.

<ContextComposer contexts={[]}>{render}</ContextComposer>

Incorrect PropTypes validation for context objects

The Provider and Consumer context components are both typed as PropTypes.func, which is incorrect: they are non-callable private component types represented by objects. This results in spurious warnings when this package is used with React's implementation of createContext.

    Warning: Failed prop type: Invalid prop `contexts[0]` supplied to `ContextComposer`.
        in ContextComposer

Non ES6 in code example?

Hi,

I think this is exactly what I need to solve my problem of too many contexts, but I don't understand the following lines of code in your example....

`import createReactContext, { type Context } from 'create-react-context';

const ThemeContext: Context = createReactContext('light');
const LanguageContext: Context = createReactContext('en');`

Am I going crazy? This doesn't look like any javascript I recognize.

docs: Give an example of "ye olde way"

Also, are there other libraries attempting to solve the annoyance of nested providers? Curious about other possible design patterns that compelled someone to build them.

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.