1.0.3 • Published 5 years ago

tonu-computed v1.0.3

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

tonu-computed

example

import computed from 'tonu-computed'
class extends React.Component{
    state = {
        num: 1
    }
    constructor(props){
        super(props)
        this.$computor = computed({
            test1(state, getter){
                return state.num * 2
            },
            test2(state, getter){
                return getter('test1') / state.num
            }
        })
        this.$computor.watch({
            test1(n, o){
                // 
            }
        })
    }
    componentDidMount(){
        setTimeout(() => {
            this.setState({num: 5}, this.$computor.$apply(this))
            // or
            const value = this.$computor.compute(this.state)
            this.setState(value)
        }, 5000)
    }

    render(){
        return (
            <ul>
                <li>test1: {this.state.test1}</li>
                <li>test1: {this.state.test2}</li>
            </ul>
        )
    }
}

// 2.修饰

@connect({
    compute: {
        demo: {
            add(state){
                return state.index++
            }
        },
        ignore: {
            add(state){
                return state.index++
            }
        }
    },
    isAll: {ignore: false}
})
class {
    // 返回结果 {add: 2, index: 1}
    demo(){
        return {index: 1}
    }
    // 返回结果 {add: 2}
    ignore(){
        return {index: 1}
    }
}