Effector-react
React bindings for effector
Installation
npm install --save effector effector-react
Or using yarn
yarn add effector effector-react
Usage

import {createStore, createStoreObject, createEvent} from 'effector'
import {createStoreConsumer} from 'effector-react'
const inputText = createEvent('input text')
const text = createStore('').on(inputText, (state, payload) => payload)
const length = createStore(0).on(inputText, (state, payload) => payload.length)
const store = createStoreObject({
text,
length,
})
const FormStore = createStoreConsumer(store)
const Form = () => (
<FormStore>
{state => (
<form>
<input
type="text"
onChange={e => inputText(e.currentTarget.value)}
value={state.text}
/>
<p>Length: {state.length}</p>
</form>
)}
</FormStore>
)
effector-react 20.0.3
- Allow
as const
typescript assertion for useStoreMap
keys. It helps us to infer type for fn
arguments
import React from 'react'
import {value createStore} from 'effector'
import {value useStoreMap} from 'effector-react'
type User = {
username: string
email: string
bio: string
}
const $users = createStore<User[]>([
{
username: 'alice',
email: 'alice@example.com',
bio: '. . .',
},
{
username: 'bob',
email: 'bob@example.com',
bio: '~/ - /~',
},
{
username: 'carol',
email: 'carol@example.com',
bio: '- - -',
},
])
export const UserProperty = ({id, field}: {id: number; field: keyof User}) => {
const value = useStoreMap({
store: $users,
keys: [id, field] as const,
fn: (users, [id, field]) => users[id][field] || null,
})
return <div>{value}</div>
}
In typescript versions below 3.4, you can still use an explicit type assertion
import React from 'react'
import {value createStore} from 'effector'
import {value useStoreMap} from 'effector-react'
type User = {
username: string
email: string
bio: string
}
const $users = createStore<User[]>([
{
username: 'alice',
email: 'alice@example.com',
bio: '. . .',
},
{
username: 'bob',
email: 'bob@example.com',
bio: '~/ - /~',
},
{
username: 'carol',
email: 'carol@example.com',
bio: '- - -',
},
])
export const UserProperty = ({id, field}: {id: number; field: keyof User}) => {
const value = useStoreMap({
store: $users,
keys: [id, field] as [number, keyof User],
fn: (users, [id, field]) => users[id][field] || null,
})
return <div>{value}</div>
}
as const in typescript docs