JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q33797F
  • License MIT

Co-located and composable state management for React and React-like libraries.

Package Exports

  • microstate

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (microstate) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

microstate

Co-located, functional state management for React. seState syntatic sugar.

js-standard-style

Usage

Wrap your application with the Provider.

import { Provider } from 'microstate'

const App = props => (
  <Provider>
    <h1>My App</h1>
  </Provider>
)

render(<App/>, root)

Connect your component to the provider state using connect. The function signature for connect looks like this:

connect(mapStateToProps[, mapDispatchToProps, initialState])(MyComp)
import { connect } from 'microstate'

const initialState = {
  count: 0
}

const mapStateToProps = state => ({
  count: state.count
})

const mapDispatchToProps = dispatch => ({
  inc: () => dispatch(state => {
    count: state.count + 1
  })
})

const Component = ({ inc, count }) => (
  <div>
    <button onClick={inc}>Increment</button>
    <span>{count}</span>
  </div>
)

export default connect(
  mapStateToProps,
  mapDispatchToProps,
  initialState
)(Component)

Then render your component within the Provider context.

import { Provider } from 'microstate'
import Component from './Component.js'

const App = props => (
  <Provider>
    <Component/>
  </Provider>
)

render(<App/>, root)

Note: the state is availabe at a component level and below during first render. However, since an initial render is required to evaluate the connect function, state will only be availabe at the application level after that first render.

Subscribing to state

Subscribing to another component's state is easy. Let's pretend the below is a different component than the counter above.

// Output.js
export default connect(
  state => ({
    count: state.count,
    text: state.text
  },
  null,
  {
    text: 'The count is'
  }
)(({ count, text }) => (
  <div>{text + ' ' + count}</div>
))

Then add it to the rendered App from above.

import { Provider } from 'microstate'
import Component from './Component.js'
import Output from './Output.js'

export default props => (
  <Provider>
    <Component/>
    <Output/>
  </Provider>
)

render(<App/>, root)

MIT License