Giter VIP home page Giter VIP logo

react-huc's Introduction

React Huc

React Huc is a container

Install

$ npm install react-huc -S

usage

Huc

import Huc} from 'react-huc'

<Huc initStore={{isShow: false}}>
  <Child1 />
  <Child2 />
</Huc>

//Change huc state from child
//Child1
render() {
  const {isShow} = this.props
  return (
    <div>{isShow}</div>
  )
}
//Chil2
render(){
  const {isShow, dispatch} = this.props
  return (
    <div>
    <button onClick={()=> dispatch({isShow: !isShow})}>toggle</button>
    </div>
  )
}
import { Huc } from 'react-huc'

handleToggle = () => {
  const parent = this.refs.parent
  parent.dispatch({isShow: !parent.state.isShow})
}
//change state from other component
<button onClick={()=> this.handleToggle()}></button>

<Huc initStore={{isSHow: false}} ref="parent">
  <Child1 />
  <Child2 />
</Huc>

Use reducer similar as redux

import Huc from 'react-huc'

const withReducer = (state, action) => {
  switch(action.type) {
    case 'TOGGLE_MODAL':
      return Object.assign({},state,{isShow: !state.isShow})
    default:
      return state
  }
}

<Huc initStore={{isShow: false}} witReducer={withReducer}>
  <Child1 />
</Huc>

//Change huc state from child
//Child1
render() {
  const {isShow, dispatch} = this.props
  return (
    <div onClick={()=> dispatch({type: 'TOGGLE_MODAL'})}>{isShow}</div>
  )
}
import Huc from 'react-huc'

const withReducer = (state, action) => {
  switch(action.type) {
    case 'TOGGLE_MODAL':
      return Object.assign({},state,{isShow: !state.isShow})
    default:
      return state
  }
}

handleToggle = () => {
  const parent = this.refs.parent
  parent.dispatch({type: 'TOGGLE_MODAL'})
}
//change state from other component
<button onClick={()=> this.handleToggle()}></button>

<Huc initStore={{isSHow: false}} withReducer={withReducer} ref="parent">
  <Child1 />
</Huc>

Provider

//app.js
improt React, {Component, PropTypes} from 'react'
import {Provider} from 'react-hoc'


class App extends Component {

  setStateWithRef = (ref, data) => {
    this.refs[ref].setState(data)
  }

  render(){
    return (
      <Provider context={{app: this}}>
      <Child />
      <Count ref="count" initState={{count: 0}}/>
      </Provider>
    )
  }
}


//children component
class Child extends Component {
  static contextTypes = {
    app: PropTypes.object.isRequired
  }
  constructor(props){
    super(props)
  }
  handleAdd = () => {
    const {app, app:{refs:{navigation}}} = this.context
    app.setStateWithRef('navigation', {count: count.state.count + 1})
  }

  render(){
    return (
      <div onClick={()=> this.handleAdd()}> + </div>
    )
  }
}

class Count extends Component {
  constructor(props){
    super(props)
  }
  componentWillMount = () => {
    if(this.props.initState){
      this.state = initState
    }
  }

  render(){
    return (
      <div>{this.props.count}</div>
    )
  }
}

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.