0.0.6 • Published 6 years ago

react-assign-component v0.0.6

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

AssignComponent

Install

npm install react-assign-component

Depends on React and rxjs

Howto

AssignComponent

import React from "react"
import { AssignComponent } from 'react-assign-component'

class TestA extends AssignComponent {
    constructor(props) {
        super(props)
        this.state ={
            name: 'cxh2',
            age: 17,
            height: 180,
        }
        this.createStore(assigner, actions)
    }

    change = async () => {
        this.a({type:'C'})
    }
    changeAge = async () => {
        this.a({type:'D'})
    }
    render() {
        return  <div>
                    <div onClick={this.change}>{this.state.name}</div>
                    <div onClick={this.changeAge}>{this.state.age}---{this.state.height}</div>
                </div>
    }
}

const actions = {
  a: function(dispatch) {
    return async (action) => {
        let res = await axios.post('...', action)
        dispatch(res)
    }
  }
}

function assigner(action, assign) {
   switch(action.type) {
        case 'A': 
            return  assign({
                name: 'jk'
            })
        case 'B':
            return  assign({
                age: 19
            })
    } 
}

AssignRXComponent

import React from "react"
import { AssignRxComponent } from 'react-assign-component'

export default class Rxc extends AssignRxComponent  {
    constructor() {
        super(...arguments)
        
        this.state = {
            name: 'rx',
            age: 19
        }
        this.createStore(actions, assigner)
    }
    change = () =>{
        this.changName({
            type: 'RXJS',
            name: 'rxjs'
        })

        this.changeAge({
            age: 20
        })
    }

    changeByAction = () => {
        this.changeNameByAction({
            type: 'RXJS',
            name: 'RXJS'
        })
    }

    render() {
        return  <div>
                    <div onClick={this.change}>{ this.state.name } -- {this.state.age}</div>
                    <div onClick={this.changeByAction}>actions change name-age</div>
                </div>
    }
    
}

const actions = {
    changName: function(observable, dispatch) {
        observable
        .map( (data) => { 
            console.log('A', data); 
            return {type: data.type, name: 'nihao'}})
        .subscribe( (d) => {
           console.log('B', d)
           dispatch(d)
        })
    },
    changeAge: function(observable, dispatch) {
        observable.subscribe( (data) => {
            this.setState(data)
        })
    },
    changeNameByAction: function(observable, dispatch) {
        observable.map(action => {
            console.log('C', action)
            return {
                type: action.type,
                name: 'RXJS-ACTION'
            } 
        })
        .subscribe( (data) => {
            console.log('D', data)
            dispatch(data)
        })
    },
}


function assigner(action, assign) {
    switch(action.type) {
        case 'RXJS': 
            return  assign( {
                name: action.name
            })
        case 'RX':
            return  assign({
                name: action.name
            })
    } 
}

Keywords

react state rxjs

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago