theKashey / react-push-channel

🎈Context is to drill props down. React Push Channel to drill props up.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-push-channel

Greenkeeper badge


NPM

Context is to drill props down. React Push Channel drill props up.

API

createChannel(initialValue, reducer?, initialValueForReducer?)

creates a channel, with given initialValue(used for typing), and optional reducer. Reducer will be applied to all stored messages, producing the result.

createChannel return an object with 3 fields:

  • Collector - to collect all messages.
    • callback - current value will be reported via callback.
    • [merge] - enabled reducer on data. Ie merges everything into a single value. Otherwise would return last value.
  • Push - put message into the channel
    • any props from initialValue
  • Pop - read the current active message. Pop doesn't remove the message(ie "pops"). Only Push component unmount removes it.

Use as React-helmet?

import {createChannel} from 'react-push-channel';

const helmet = createChannel({
  title: '',
  description: ''
});

// the root collector
<helmet.Collector 
  callback={helmet => this.setState({helmet})} // transfer reported info into the state 
  merge                                        // merge all data in reverse order 
>
   <helmet.Push title="Page Title"/>
   <helmet.Push description="Page description"/>
</helmet.Collector>

// or actually do the job

<helmet.Collector 
  callback={helmet => document.title=helmet.title} // actually SET THE TITLE! 
  merge                                         
>
   <helmet.Push title="Page Title"/>
   <helmet.Push description="Page description"/>
</helmet.Collector

React-Lock

This example uses reducer to basically calculate number or active locks, and Pop to read active value down the tree.

const Lock = createChannel({}, acc => acc + 1, 0);

<Lock.Collector merge callback={locked => this.setState({locked: !!locked})}>
  <Lock.Push />
  <Lock.Pop>{locked => <span> is {locked?'locked':'unlocked'}</Lock.Pop>
  // ^^ would be 1 and `locked`
</Lock.Collector>  

Licence

MIT

About

🎈Context is to drill props down. React Push Channel to drill props up.


Languages

Language:TypeScript 97.1%Language:HTML 2.9%