npm install retreon---
Retreon aims to provide good patterns and strong types out of the box, including tools for async actions and error handling. Retreon is FSA compliant.
Here's a taste:
``typescript/resources/${id}
// actions.ts
const fetchResource = createAction.async('fetch-resource', async (id: number) => {
const response = await fetch()`
return response.json()
})
`typescript
// reducer.ts
const reducer = createReducer(initialState, handleAction => [
// Called as soon as the action starts
handleAction.optimistic(fetchResource, (state, resourceId) => {
state.loading = true
}),
// Optionally, handle errors if your action fails
handleAction.error(fetchResource, (state, error) => {
state.loading = false
}),
// Called when the action's promise resolves, passing the payload
handleAction(fetchResource, (state, resource) => {
state.loading = false
state.resource = resource
}),
])
`
If you prefer to learn by example, take a gander at the examples directory, or check out TodoMVC to see a functioning application.
`bashNPM
npm install retreon
Retreon depends on middleware to implement async actions and error handling. Once it's installed, register it with your redux store:
`ts
// Wherever you create your redux store...
import { middleware } from 'retreon'createStore(reducer, applyMiddleware(middleware))
``---
Retreon is inspired by redux-actions and immer.