effector-react
React bindings for effector
Installation
npm install --save effector effector-react
Or using yarn
yarn add effector effector-react
Usage
import {createStore, combine, createEvent} from 'effector'
import {useUnit} from 'effector-react'
const inputText = createEvent()
const $text = createStore('').on(inputText, (_, text) => text)
const $size = $text.map(text => text.length)
const Form = () => {
const {text, size} = useUnit({
text: $text,
size: $size,
})
const handleTextChange = useUnit(inputText)
return (
<form>
<input
type="text"
onChange={e => handleTextChange(e.currentTarget.value)}
value={text}
/>
<p>Length: {size}</p>
</form>
)
}
Try it
useUnit in docs
Units in docs
createStore in docs
createEvent in docs
effector-react 22.1.0
- Added support for react 18 (PR #655)
- Added
useUnit
method to read multiple stores and bind events or effects to scope in a single batched call (PR #733, #738)
import {value createEvent, value createStore, value fork} from 'effector'
import {value useUnit, value Provider} from 'effector-react/scope'
const inc = createEvent()
const $count = createStore(0)
const $title = createStore('useStore example')
$count.on(inc, x => x + 1)
const App = () => {
const [count, title, incFn] = useUnit([$count, $title, inc])
return (
<>
<h1>{title}</h1>
<p>Count: {count}</p>
<button onClick={() => incFn()}>increment</button>
</>
)
}
const scope = fork()
render(
() => (
<Provider value={scope}>
<App />
</Provider>
),
document.getElementById('root'),
)
- Added
placeholder
option to useList
to render in cases of empty list
const ChatList = () => (
<div>
{useList($chats, {
fn: chat => <div>Chat {chat.name}</div>,
keys: [],
placeholder: <div>You have no chats yet. Add first one?</div>,
})}
</div>
)
- Added
defaultValue
option to useStoreMap
to return in cases when fn
returns undefined
const ChatName = ({id}) => {
const chat = useStoreMap({
store: $chats,
keys: [id],
fn: chats => chats.find(chat => chat.id === id),
defaultValue: {id: 'default', name: 'Default chat'},
})
return <span>{chat.name}</span>
}
- Fixed
Gate.status
store being serialized (PR #683)