Hydux
A light-weight Elm-like alternative for Redux ecosystem, inspired by Hyperapp and Elmish.
Contents
Features
- Elm Architecture, split your whole app with init, state, actions.
- hyperapp compatible API
- Elm-like side effect manager and subscribe API
- Support any vdom library, including react (official support)
- Router (Recommended)
- Official support for react-router
- Hot reload (hmr)
- Server-Side Rendering(SSR)
- code splitting, seamlessly integrated with SSR.
- logger, persist, Redux Devtools with time traveling, ultradom(1kb vdom), **All in One**, easily setup all these fancy stuff without pain!
Try it online!
Install
yarn add hydux # or npm i hydux
React-hooks like dependency injection for state/actionsNew!
This is an experimental dependency injection API for actions inspired by react-hooks, totally downward compatible, with this we don't need curring to inject state and actions or manually markup types for the return value any more!
yarn add hydux@^v0.5.8
Quick Example
Let's say we got a counter, like this.
// Counter.js count: 1 actions: count: statecount - 1 count: statecount + 1 <div> <h1>statecount</h1> <button onclick=actionsdown>–</button> <button onclick=actionsup>+</button> </div>
Then we can compose it in Elm way, you can easily reuse your components.
// use built-in 1kb ultradom to render the view.let app = _app const actions = counter1: Counteractions counter2: Counteractions const state = counter1: Counter counter2: Counter const view = state: State actions: Actions <main> <h1>Counter1:</h1> Counter <h1>Counter2:</h1> Counter </main> state actions view
Init with Command
You can init the state of your app via plain object, or with side effects, like fetch remote data.
If we want to init a child component with init command, we need to map it to the sub level via lambda function, just like type lifting in Elm.
// App.tsx
This might be too much boilerplate code, but hey, we provide a type-friendly helper function! See:
// Combine all sub components's init/actions/views, auto map init commands.
Actions with Command
This library also implemented a Elm-like side effects manager, you can simple return a record with state, cmd in your action e.g.
app
Parent-Child Components Communication
In Elm, we can intercept child component's message in parent component, because child's update function is called in parent's update function. But how can we do this in hydux?
const Cmd = Hydux { return state: counter1: Counter counter2: Counter }const actions = counter2: counteractions counter1: counteractionsHyduxtype State = ReturnType<typeof init>'state'type Actions = typeof actionslet ctx = Hyduxapp<State Actions> initState actions view: noop onRender: noop
Documentation
Ecosystem
Libraries
- hydux-react: Hydux's react integration
- hydux-preact: Hydux's preact integration
- hydux-react-router: Hydux's react-router integration
- hydux-mutator: A statically-typed immutable update help package, which also contains immutable collections.
- hydux-transitions: A css transition library inspired by animajs's timeline, follow The Elm Architecture.
- hydux-data: Statically-typed data-driven development for hydux, in the Elm way. Inspired by apollo-client.
- hydux-pixi: High performance pixi.js renderer for Hydux.
- hydux code snippets for vscode (TS) Best practice for using hydux + typescript without boilerplate code.
Samples
- samples-antd: Admin sample in hydux.
Counter App
git clone https://github.com/hydux/hydux.gitcd hyduxyarn # or npm i cd examples/counteryarn # or npm i npm start
Now open http://localhost:8080 and hack!
Why
After trying Fable + Elmish for several month, I need to write a small web App in my company, for many reasons I cannot choose some fancy stuff like Fable + Elmish, simply speaking, I need to use the mainstream JS stack but don't want to bear Redux's cumbersome, complex toolchain, etc anymore.
After some digging around, hyperapp looks really good to me, but I quickly find out it doesn't work with React, and many libraries don't work with the newest API. So I create this to support different vdom libraries, like React(official support), ultradom(built-in), Preact, inferno or what ever you want, just need to write a simple enhancer!
Also, to avoid breaking change, we have built-in support for HMR, logger, persist, Redux Devtools, you know you want it!
License
MIT