useStore

React hook, which subscribes to store and returns its current value, so when store is updated, the component will update automatically.

useStore(store: Store<T>): T

Arguments

  1. store: Store

Returns

(State)

Example

import {createStore, createApi} from 'effector'
import {useStore} from 'effector-react'
const counter = createStore(0)
const {increment, decrement} = createApi(counter, {
increment: state => state + 1,
decrement: state => state - 1,
})
export const Counter = () => {
const state = useStore(counter)
return (
<div>
{state}
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
)
}

Try it

Last updated on