GithubHelp home page GithubHelp logo

bounce's Introduction

Bounce

The uncomplicated state management library for Yew.

Bounce is inspired by Redux and 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 subscribes to that state will be notified.

  • Reduces Cloning.

    States are Rc'ed.

Example

For bounce states to function, a <BounceRoot /> must be registered.

#[function_component(App)]
fn app() -> Html {
    html! {
        <BounceRoot>
            {children}
        </BounceRoot>
    }
}

A simple state is called an Atom.

You can derive Atom for any struct that implements PartialEq and Default.

#[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.

#[function_component(Setter)]
fn setter() -> Html {
    let username = use_atom::<Username>();

    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! {
        <div>
            <input type_="text" oninput={on_text_input} value={username.inner.to_string()} />
        </div>
    }
}

If you wish to create a read-only (or set-only) handle, you can use use_atom_value (or use_atom_setter).

#[function_component(Reader)]
fn reader() -> Html {
    let username = use_atom_value::<Username>();

    html! { <div>{"Hello, "}{&username.inner}</div> }
}

You can find the full example here.

Complex State

If you wish to have a reducer-based state that changes based on actions, you can use a Slice.

It shares the same API as the Reducible trait for yew, but is initialised with Default.

You can use it with use_slice, use_slice_value and use_slice_dispatch hooks.

You can find an example of slices here.

License

Bounce is dual licensed under the MIT license and the Apache License (Version 2.0).

bounce's People

Contributors

futursolo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.