effector-react
Advanced tools
Changelog
effector 21.0.3, effector-react 21.0.4, effector-vue 21.0.3
Changelog
effector 21.0.0
split
for pattern-matching without additional forwardsimport {split, createEffect, createEvent} from 'effector'
const messageReceived = createEvent()
const showTextPopup = createEvent()
const playAudio = createEvent()
const reportUnknownMessageTypeFx = createEffect({
handler({type}) {
console.log('unknown message:', type)
},
})
split({
source: messageReceived,
match: {
text: msg => msg.type === 'text',
audio: msg => msg.type === 'audio',
},
cases: {
text: showTextPopup,
audio: playAudio,
__: reportUnknownMessageTypeFx,
},
})
showTextPopup.watch(({value}) => {
console.log('new message:', value)
})
messageReceived({
type: 'text',
value: 'Hello',
})
// => new message: Hello
messageReceived({
type: 'image',
imageUrl: '...',
})
// => unknown message: image
You can match directly to store api as well:
import {split, createStore, createEvent, createApi} from 'effector'
const messageReceived = createEvent()
const $textContent = createStore([])
split({
source: messageReceived,
match: {
text: msg => msg.type === 'text',
audio: msg => msg.type === 'audio',
},
cases: createApi($textContent, {
text: (list, {value}) => [...list, value],
audio: (list, {duration}) => [...list, `audio ${duration} ms`],
__: list => [...list, 'unknown message'],
}),
})
$textContent.watch(messages => {
console.log(messages)
})
messageReceived({
type: 'text',
value: 'Hello',
})
// => ['Hello']
messageReceived({
type: 'image',
imageUrl: '...',
})
// => ['Hello', 'unknown message']
messageReceived({
type: 'audio',
duration: 500,
})
// => ['Hello', 'unknown message', 'audio 500 ms']
effector/fork
into effector
. Now all methods required for SSR are exported from the library itself, making effector/fork
an aliasScope
type alias for Fork
import {createStore} from 'effector/effector.mjs'
console.error
with undefined return, which was violating the type of effectrestore
aliases, event.filter(fn)
alias for event.filterMap(fn)
, greedy
in sample
as separate last argument and unused blocks
and Kind
Changelog
effector-react 20.9.0
useGate
with fork
support from effector-react/ssr
import {value useGate, value useStore, value Provider} from 'effector-react/ssr'
import {value createGate} from 'effector-react'
import {value createDomain, value forward} from 'effector'
import {value fork} from 'effector/fork'
const app = createDomain()
const activeChatGate = createGate({domain: app})
const getMessagesFx = app.createEffect({
async handler({chatId}) {
return ['hi bob!', 'Hello, Alice']
},
})
const $messagesAmount = app
.createStore(0)
.on(getMessagesFx.doneData, (_, messages) => messages.length)
forward({
from: activeChatGate.open,
to: getMessagesFx,
})
const ChatPage = ({chatId}) => {
useGate(activeChatGate, {chatId})
return (
<div>
<header>Chat {chatId}</header>
<p>Messages total: {useStore($messagesAmount)}</p>
</div>
)
}
const App = ({root}) => (
<Provider value={root}>
<ChatPage chatId="chat01" />
</Provider>
)
const scope = fork(app)
ReactDOM.render(<App root={scope} />, document.getElementById('root'))
domain
optional field to createGate
which will be used to create gate units (useful for ssr)createGate({domain}) in documentation
useList
hook typings for typescript exported from effector-react/ssr
by allowing usage as components' return value (fix DefinitelyTyped issue)Changelog
effector-react 20.8.0
Gate
state in createGate
via defaultState
fieldcreateGate({defaultState}) in documentation
object
restriction from createGate
Props
type in typescript, as it becomes useless with introduction of useGate
. This code now passes type checking successfullyimport {value createGate} from 'effector-react'
const RouteGate = createGate<string>()
const UserGate = createGate({defaultState: 'guest'})
Changelog
effector-react 20.7.3, effector-vue 20.4.2
effector-react/compat
and effector-vue/compat
compatibility with IE11Changelog
effector-react 20.7.1
useList
hook typings for typescript by allowing usage as components' return value (fix DefinitelyTyped issue)This code now works without type errors:
import {value createStore} from 'effector'
import {value useList} from 'effector-react'
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: '- - -',
},
])
const UserList = () => useList($users, ({username}) => <p>{username}</p>)
const App = () => (
<div>
<UserList />
</div>
)