Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bounce-rs/bounce
The uncomplicated Yew State management library
https://github.com/bounce-rs/bounce
rust state-management wasm yew
Last synced: 4 days ago
JSON representation
The uncomplicated Yew State management library
- Host: GitHub
- URL: https://github.com/bounce-rs/bounce
- Owner: bounce-rs
- License: apache-2.0
- Created: 2021-11-24T09:07:29.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-22T19:20:50.000Z (10 months ago)
- Last Synced: 2024-04-14T11:09:28.991Z (7 months ago)
- Topics: rust, state-management, wasm, yew
- Language: Rust
- Homepage: https://bounce-rs.org
- Size: 321 KB
- Stars: 98
- Watchers: 2
- Forks: 10
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
- awesome-ccamel - bounce-rs/bounce - The uncomplicated Yew State management library (Rust)
- awesome-yew - Bounce - The uncomplicated state management library for Yew, inspired by [Redux](https://github.com/reduxjs/redux) and [Recoil](https://github.com/facebookexperimental/Recoil). (Crates / Hooks)
README
# Bounce
[![Run Tests & Publishing](https://github.com/bounce-rs/bounce/actions/workflows/everything.yml/badge.svg)](https://github.com/bounce-rs/bounce/actions/workflows/everything.yml)
[![crates.io](https://img.shields.io/crates/v/bounce)](https://crates.io/crates/bounce)
[![docs.rs](https://docs.rs/bounce/badge.svg)](https://docs.rs/bounce/)The uncomplicated state management library for Yew.
Bounce is inspired by [Redux](https://github.com/reduxjs/redux) and
[Recoil](https://github.com/facebookexperimental/Recoil).## Rationale
Yew state management solutions that are currently available all have
some (or all) of the following limitations:- Too much boilerplate.
Users either have to manually control whether to notify
subscribers or have to manually define contexts.- State change notifies all.
State changes will notify all subscribers.
- Needless clones.
A clone of the state will be produced for all subscribers whenever
there's a change.Bounce wants to be a state management library that:
- Has minimal boilerplate.
Changes are automatically detected via `PartialEq`.
- Only notifies relevant subscribers.
When a state changes, only hooks that subscribe to that state will
be notified.- Reduces Cloning.
States are `Rc`'ed.
## Example
For bounce states to function, a `` must be registered.
```rust
#[function_component(App)]
fn app() -> Html {
html! {
{children}
}
}
```A simple state is called an `Atom`.
You can derive `Atom` for any struct that implements `PartialEq` and `Default`.
```rust
#[derive(PartialEq, Atom)]
struct Username {
inner: String,
}impl Default for Username {
fn default() -> Self {
Self {
inner: "Jane Doe".into(),
}
}
}
```You can then use it with the `use_atom` hook.
When an `Atom` is first used, it will be initialised with its `Default`
value.```rust
#[function_component(Setter)]
fn setter() -> Html {
let username = use_atom::();let on_text_input = {
let username = username.clone();Callback::from(move |e: InputEvent| {
let input: HtmlInputElement = e.target_unchecked_into();username.set(Username { inner: input.value().into() });
})
};html! {
}
}
```If you wish to create a read-only (or set-only) handle, you can use
`use_atom_value` (or `use_atom_setter`).```rust
#[function_component(Reader)]
fn reader() -> Html {
let username = use_atom_value::();html! {
{"Hello, "}{&username.inner}}
}
```You can find the full example [here](https://github.com/futursolo/bounce/blob/master/examples/simple/src/main.rs).
## License
Bounce is dual licensed under the MIT license and the Apache License (Version 2.0).