staat-react
TypeScript icon, indicating that this package has built-in type declarations

1.3.0-beta.1 • Public • Published

Staat/React

The React bindings for Staat

Basic usage

import staat from 'staat';
import reactStaat from 'staat-react';
import React from 'react';
import ReactDOM from 'react-dom';
 
const initialState = {
  count: 0
};
 
const state = staat(
  {
    add(currentState: typeof initialState, value: number) {
      return { ...currentState, count: currentState.count + value };
    },
    subtract(currentState: typeof initialState, value: number) {
      return { ...currentState, count: currentState.count - value };
    }
  },
  initialState
);
 
const { Provider, connect } = reactStaat(state);
 
const Calculator: React.StatelessComponent<CalculatorProps> = props => {
  return (
    <div>
      <button onClick={() => props.add(10)}>Add</button>
      <button onClick={() => props.subtract(3)}>Subtract</button>
      <div>{props.count}</div>
    </div>
  );
};
 
type TransformerProps = {
  add: typeof state.add;
  subtract: typeof state.subtract;
};
 
type OwnProps = {};
 
type CalculatorProps = TransformerProps & typeof initialState & OwnProps;
 
const Calc = connect<OwnProps, typeof initialState, TransformerProps>(
  ({ calculator }) => {
    return {
      count: calculator.count
    };
  },
  () => {
    return {
      add: calculator.add,
      subtract: calculator.subtract
    };
  }
)(Calculator);
 
ReactDOM.render(
  <Provider>
    <Calc />
  </Provider>,
  document.getElementById('entry')
);

Readme

Keywords

none

Package Sidebar

Install

npm i staat-react

Weekly Downloads

9

Version

1.3.0-beta.1

License

ISC

Unpacked Size

79.2 kB

Total Files

27

Last publish

Collaborators

  • ericmackrodt