Coder Social home page Coder Social logo

react-push-channel's Introduction

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

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.