0.2.1 • Published 7 years ago

react-combine-consumers v0.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

react-combine-consumers

this is a 2KB lib for React new Context Api

there are Component:combineConsumers and HOC:withConsumers

Install

npm:

npm install --save-dev react-combine-consumers

Get Started

combineConsumers( Consumers )

A multiple context.consumer combine to one component

in React Developer Tool show as combine(...)

<Context.Provider value="app">
  <Context.Provider value={height:16}>
    ...
    <combine(name,style)>
      <Context.Consumer>
        <Context.Consumer>
          <button style={height:16} >
            <span>app</span>
          </button>
        <Context.Consumer>
      </Context.Consumer>
    </combine(name,style)>
    ...
  </Context.Provider>
</Context.Provider>

Consumers

a object, value is the Context.Consumer, context value will marked as it's own key;

const Consumers = {
  name: NameContext.Consumer,
  style: StyleContext.Consumer,
}

Usage

import React from 'react';
import { combineConsumers } from 'react-combine-consumers';
import ColorContext from './Themecontext';
import HeightContext from './Themecontext';

const StyleConsumer = combineConsumers({color:ColorContext.Consumer,height:HeightContext.Consumer})

export default ()=>{
  return <HeightContext.Provider value="19px">
    <ColorContext.Provider value="#2196f3">
      <StyleConsumer>
        {
          ({color,height})=>
            <button style={{color,height}}>
              this is a button
            </button>
        }
      </StyleConsumer>
    </ColorContext.Provider>
  </HeightContext.Provider>
}

withConsumers( Consumers )

this can combine multiple consumers as a High-Order-Component

in React Developer Tool show as with(...)

<Context.Provider value="app">
  <Context.Provider value={height:16}>
    ...
    <with(name,style)>
      <combine(name,style)>
        <Context.Consumer>
          <Context.Consumer>
            <SomeComponent name="app" style={height:16}/>
          <Context.Consumer>
        </Context.Consumer>
      </combine(name,style)>
    </with(name,style)>
    ...
  </Context.Provider>
</Context.Provider>

Consumers

a object, value is the Context.Consumer, context value will marked as it's own key;

const Consumers = {
  name: NameContext.Consumer,
  style: StyleContext.Consumer,
}

Usage

import React from 'react';
import { withConsumers } from 'react-combine-consumers';
import ColorContext from './Themecontext';
import HeightContext from './Themecontext';
import Button from './Button';

const EnhancedButton = withConsumers({color:ColorContext.Consumer,height:HeightContext.Consumer})(Button)

export default ()=>{
  return <HeightContext.Provider value="19px">
    <ColorContext.Provider value="#2196f3">
      <EnhancedButton/>
    </ColorContext.Provider>
  </HeightContext.Provider>
}

updates

v0.2.0

main update: reduce module size

versionstatparsedgzip
0.1.27.14KB6.95KB1.49KB
0.2.02.12KB2.24KB977B
0.2.1

7 years ago

0.2.0

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago