hydux-data
Data-driven appication in hydux, in the Elm way, inspired by apollo-client.
Install
yarn add hydux-data
Usage
This package can make your daily work easier. When you get lot's of views which just rendering some data from server, with less user interaction, this package will add isLoading flag and fetch error handler automatically for your each fetch function.
Let's say we already get an api function, like this fake one, it takes some parameters and return a promise can resolve as a data, or reject as an error (message
propertie is required and would be used later).
const asyncApi = {
fetchCount(count: number, failed = false) {
return new Promise<number>(
(resolve, reject) =>
setTimeout(
() => {
failed
? reject(new Error(`Fetch ${count} failed!`))
: resolve(count)
},
10,
)
)
},
}
Now we can make a loadable api to generate some state and actions.
import Loadable from 'hydux-data'
const loadableApi = Loadable({
fetchCount: {
init: 0,
api: asyncApi.fetchCount,
},
})
const ctx = Hydux.app<typeof loadableApi.state, typeof loadableApi.actions>({
init: () => loadableApi.state,
actions: loadableApi.actions,
view: (state, actions) => {
return
},
})
Here is the generated state and actions
state = {
fetchCount: {
isLoading: false,
data: 0,
error: '',
rawError: null,
}
}
actions = {
fetchCount: (count: number, failed = false) => any
disableLoadingFlag: () => any
enableLoadingFlag: () => any
}
Example App
git clone https://github.com/hydux/hydux-data.git
cd examples/counter
yarn
npm start
Now open http://localhost:8080 and hack!
License
MIT