0.13.9 • Published 5 years ago

rc-flex-store v0.13.9

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

rc-flex-store

A flexible state store for React component.

NPM Version Dependencies DevDependencies

API

create(initialState: React.ComponentState, updaters?: { updater: string: any }, name?: string): Store;

  • Create a store.

mount(store: Store, mapStoreToProps?: (store: UserStore, state: StoreState, props: Props) => Props, forwardRef?: boolean): React.Component

  • Mount a store provider to react component.

connect(store: Store, mapStoreToProps?: (store: UserStore, state: StoreState, props: Props) => Props;, forwardRef?: boolean): React.Component

  • Connect react component to a store consumer.

Usage

import ReactDOM from 'react-dom';
import React, { Fragment } from 'react';
import { create, mount, connect } from 'rc-flex-store';

const counter = create(
  {
    count: 0
  },
  {
    increment() {
      this.setState({ count: this.state.count + 1 });
    },
    decrement() {
      this.setState({ count: this.state.count - 1 });
    }
  },
  'counter'
);

@connect(
  counter,
  (store, { count }) => ({ count })
)
class CounterView extends React.PureComponent {
  render() {
    const { count } = this.props;

    return <div> {count} </div>;
  }
}

@connect(
  counter,
  ({ decrement, increment }) => ({ decrement, increment })
)
class CounterAction extends React.PureComponent {
  render() {
    const { decrement, increment } = this.props;

    return (
      <div>
        <button onClick={decrement}>-</button>
        <button onClick={increment}>+</button>
      </div>
    );
  }
}

@mount(counter)
class Counter extends React.Component {
  render() {
    return (
      <Fragment>
        <CounterView />
        <CounterAction />
      </Fragment>
    );
  }
}

ReactDOM.render(<Counter />, document.getElementById('app'));

Example

Online Demo

Support

Support React >= 16.3.0, if use React < 16.3.0 please add React.createContext polyfill. See create-react-context.

0.13.9

5 years ago

0.13.8

5 years ago

0.13.7

5 years ago

0.13.6

5 years ago

0.13.5

5 years ago

0.13.4

6 years ago

0.13.3

6 years ago

0.13.2

6 years ago

0.13.1

6 years ago

0.13.0

6 years ago

0.12.1

6 years ago

0.12.0

6 years ago

0.11.0

6 years ago

0.10.0

6 years ago

0.9.1

6 years ago

0.9.0

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.1

6 years ago

0.6.0

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.3

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago

0.0.0

6 years ago