Skip to content

Latest commit

 

History

History
41 lines (30 loc) · 1.05 KB

State.md

File metadata and controls

41 lines (30 loc) · 1.05 KB

State

The State component is the most generic component possible, exposing to the children function the state and setState, wrapped in a classic React.Component class.

import { State } from 'react-powerplug'
<State initial={{ isLoading: false, data: null }}>
  {({ state, setState }) => {
    const onStart = data => setState({ isLoading: true })
    const onFinish = data => setState({ data, isLoading: false })

    return (
      <DataReceiver data={state.data} onStart={onStart} onFinish={onFinish} />
    )
  }}
</State>

State Props

initial = {} (optional)
Specifies the initial state, must be an object. By default, the initial state is an empty object.

onChange (optional)
The onChange event of the State is called whenever the state changes.

State Children Props

TL;DR: { state, setState }

state
object
Your state

setState
(object | (state: object) => object) => void
State setter. Is the setState from React.Component.