项目作者: theKashey

项目描述 :
🎈Context is to drill props down. React Push Channel to drill props up.
高级语言: TypeScript
项目地址: git://github.com/theKashey/react-push-channel.git
创建时间: 2018-08-15T12:51:25Z
项目社区:https://github.com/theKashey/react-push-channel

开源协议:

下载


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?

  1. import {createChannel} from 'react-push-channel';
  2. const helmet = createChannel({
  3. title: '',
  4. description: ''
  5. });
  6. // the root collector
  7. <helmet.Collector
  8. callback={helmet => this.setState({helmet})} // transfer reported info into the state
  9. merge // merge all data in reverse order
  10. >
  11. <helmet.Push title="Page Title"></helmet.Push>
  12. <helmet.Push description="Page description"></helmet.Push>
  13. </helmet.Collector>
  14. // or actually do the job
  15. <helmet.Collector
  16. callback={helmet => document.title=helmet.title} // actually SET THE TITLE!
  17. merge
  18. >
  19. <helmet.Push title="Page Title"></helmet.Push>
  20. <helmet.Push description="Page description"></helmet.Push>
  21. </helmet.Collector

React-Lock

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

  1. const Lock = createChannel({}, acc => acc + 1, 0);
  2. <Lock.Collector merge callback={locked => this.setState({locked: !!locked})}>
  3. <Lock.Push ></Lock.Push>
  4. <Lock.Pop>{locked => <span> is {locked?'locked':'unlocked'}</Lock.Pop>
  5. // ^^ would be 1 and `locked`
  6. </Lock.Collector>

Licence

MIT