1.3.3 • Published 3 years ago

@royjs/core v1.3.3

Weekly downloads
48
License
MIT
Repository
github
Last release
3 years ago

Roy buildStatus

A powerful mvvm framework for react.

Install

npm install @royjs/core --save

Motive

image

The state management is nothing more than changing the state from partial to partial sharing, so in an application, each component can be managed corresponding to a state, and only when this part needs to be shared, it is extracted.

Usage

Basic Usage

import {Store, inject} from '@royjs/core';

const store = new Store({
    state: {
        count: 0
    },
    actions: {
        add(state, payload) {
            const {count} = state;
            this.set('count', count + 1);
        },
        reduce(state, payload) {
            const {count} = state;
            this.set('count', count - 1);
        }
    }
});

@inject(store)
class App extends React.Component {
    render() {
        const {count} = this.store.state;
        return <div onClick={() => this.store.dispatch('add')}>{count}</div>
    }
}

Centralized Store

import {Store, connect} from '@royjs/core';

const store = new Store({}, {
    plugins: [devtools]
});

store.create('module1', {
    state: {
        name: 'module1'
    },
    actions: {
        change(state, payload){
            state.set('name', payload);
        }
    }
});

store.create('module2', {
    state: {
        name: 'module2'
    },
    actions: {
        change(state, payload){
            state.set('name', payload);
        }
    }
});

@connect(state => state.module1)
class App extends React.Component {
    onClick = () => {
        this.store.dispatch('module2.change', 'changed name from module1');
    }
    render() {
        return <div onClick={this.onClick}>{this.props.name}</div>
    }
}

@connect(state => state.module2)
class App2 extends React.Component {
    render() {
        return <div>{this.props.name}</div>
    }
}

Merge localStore to globalStore

import {Store, inject, connect} from '@royjs/core';

const store = new Store();

const subModuleStore = new Store({
    state: {
        name: 'subModule'
    },
    actions: {
        change(state) {
            state.set('name', 'subModuleChanged');
        }
    }
})
@inject(subModuleStore)
class SubModule extends React.Component {
    render() {
        return <div onClick={this.store.change}>{this.store.state.name}</div>
    }
}

store.mount('subModule', subModuleStore);

@connect(state => state.subModule)
class App extends React.Component {
    render() {
        return <div>{this.props.name}</div>
    }
}

Async Request

import {Store, inject} from '@royjs/core';

const store = new Store({
    state: {
        count: 0
    },
    actions: {
        add(state, payload) {
            const {count} = state;
            state.set('count', count + 1);
        },
        reduce(state, payload) {
            const {count} = state;
            state.set('count', count - 1);
        },
        fetch(state, payload) {
            this.request('./url').then(ret => {
                state.set('dataSource', ret.ds)
            });
        }
    }
});

@inject(store)
class App extends React.Component {
    componentDidMount() {
        this.store.dispatch('fetch');
    }
    render() {
        const {dataSource} = this.store.state;
        return <div onClick={() => this.store.dispatch('add')}>{dataSource}</div>
    }
}

Benchmark

Test on my macbook pro (Intel Core i7 2.2GHz)

benchmark

tnpm run benchmark
2.0.7

3 years ago

2.0.6

3 years ago

2.0.5

3 years ago

2.0.4

4 years ago

2.0.3

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.3.1

4 years ago

1.3.0

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.2

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

0.7.1

5 years ago

1.0.0

5 years ago

0.7.0

5 years ago

0.6.14

5 years ago

0.6.13

5 years ago

1.0.0-beta6

5 years ago

0.6.12

5 years ago

1.0.0-beta5

5 years ago

1.0.0-beta4

5 years ago

0.6.11

5 years ago

0.6.10

5 years ago

1.0.0-beta3

5 years ago

1.0.0-beta2

5 years ago

1.0.0-beta1

5 years ago

1.0.0-beta0

5 years ago

0.6.9

5 years ago

0.6.8

6 years ago

0.6.7

6 years ago

0.6.6

6 years ago

0.6.5

6 years ago

0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago