Skip to main content

withState

withState(opt)(Component)

A higher order component that injects state into the wrapped component.

withState options

PropertyDescription
asPropSpecify what prop to pass the state to the wrapped component as.
fromWhat store to retrieve state from.
pathThe state path to retrieve. Specified as a dotted path string, with support for arrays. Sample: "my.field.path[10].name"
selectA function that selects the required state.
constantWhen true, the State component will query state and render only once.
strictWhen true, the State will track accessed keys on every update instead of on just the first one.
defaultThe default value when the state is undefined.
throttleMilliseconds to throttle change requests
debounceMilliseconds to debounce change requests
debugWhen true, log messages regarding state changes will be printed to the console.
verboseWhen true, verbose log messages are printed to the console.
idDebug log uses this to identify components
onMountCalled with the current state when the component mounts.
onUnmountCalled with the current state when the component unmounts.
import { withState } from "carry-on-react";

const App = withState({ path: "counter", asProp: "counter" })(props => (
<div>{counter}</div>
));

The properties used to pass state to the wrapped component are determined by:

  • The asProp option, if specified
  • If the state or select result is an object it will be spread onto multiple props
  • When the state isn't an object it will be passed as the state property.

Normally you would always use the path or select options when using withState because without them your component would render on every state change:

import { withState } from "carry-on-react";

const App = withState()(props => <div>I render every state change</div>);