All Projects → Merlin04 → niue

Merlin04 / niue

Licence: MIT license
A tiny shared state and event library for React

Programming Languages

typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to niue

Constate
React Context + State
Stars: ✭ 3,519 (+20600%)
Mutual labels:  state-management, react-state, react-hooks
atomic-state
A decentralized state management library for React
Stars: ✭ 54 (+217.65%)
Mutual labels:  state-management, react-hooks
hookstore
Hook based and lightweight centralized state management for React.
Stars: ✭ 28 (+64.71%)
Mutual labels:  state-management, react-hooks
react-mlyn
react bindings to mlyn
Stars: ✭ 19 (+11.76%)
Mutual labels:  state-management, react-hooks
concave
🧐 Lens-like state management (for React).
Stars: ✭ 13 (-23.53%)
Mutual labels:  state-management, react-hooks
use-query-string
🆙 A React hook that serializes state into the URL query string
Stars: ✭ 50 (+194.12%)
Mutual labels:  state-management, react-hooks
use-simple-store
🏬 Simple state management using React Hook
Stars: ✭ 21 (+23.53%)
Mutual labels:  state-management, react-hooks
react-cool-form
😎 📋 React hooks for forms state and validation, less code more performant.
Stars: ✭ 246 (+1347.06%)
Mutual labels:  state-management, react-hooks
Easy Peasy
Vegetarian friendly state for React
Stars: ✭ 4,525 (+26517.65%)
Mutual labels:  state-management, react-hooks
React Easy State
Simple React state management. Made with ❤️ and ES6 Proxies.
Stars: ✭ 2,459 (+14364.71%)
Mutual labels:  state-management, react-state
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-23.53%)
Mutual labels:  state-management, react-hooks
use-app-state
🌏 useAppState() hook. that global version of setState() built on Context.
Stars: ✭ 65 (+282.35%)
Mutual labels:  state-management, react-hooks
resynced
An experimental hook that lets you have multiple components using multiple synced states using no context provider
Stars: ✭ 19 (+11.76%)
Mutual labels:  state-management, react-hooks
jedisdb
redis like key-value state management solution for React
Stars: ✭ 13 (-23.53%)
Mutual labels:  state-management, react-hooks
east-store
east-store is a state manager with easiest api that based hooks and immer.
Stars: ✭ 18 (+5.88%)
Mutual labels:  state-management, react-hooks
react-store
A library for better state management in react hooks world
Stars: ✭ 34 (+100%)
Mutual labels:  state-management, react-hooks
react-without-redux
React State Management without Redux
Stars: ✭ 33 (+94.12%)
Mutual labels:  state-management, react-hooks
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (+64.71%)
Mutual labels:  state-management, react-hooks
veact
Mutable state enhancer library for React based on @vuejs
Stars: ✭ 62 (+264.71%)
Mutual labels:  react-state, react-hooks
fs2-es
Event sourcing utilities for FS2
Stars: ✭ 75 (+341.18%)
Mutual labels:  events, state-management

Niue

A tiny shared state and event library for React

Niue is a small library (~1.0kb before compression) that provides a simple way to manage your React app's shared state and send events between components.

Why Niue?

Installation

yarn add niue

Managing shared state

To create a store (a thing to hold an object of state), use the createState function outside of a component:

import { createState } from 'niue';

const [useState, setState] = createState(
    // Initial value
    { count: 0, name: "foo" },
);

The resulting useState hook can be called in your component to get the latest state value:

function Counter() {
    const state = useState();

    return (
        <div>
            <p>Hello, {state.name}!</p>
            {state.count}
        </div>
    );
}

useState also accepts an optional parameter to specify which properties of the state object to "subscribe" to. Changes of these properties will cause a re-render of the component. If you don't specify anything, the entire state object will be watched; if you specify null, nothing will be watched and no re-renders will occur when state changes.

// Subscribe to only the `count` property
const state = useState(["count"]);

// Don't subscribe to anything
const state = useState(null);

Here's an example of subscribing to a single property:

import { useState } from "./Counter";

function CountDisplay() {
    const state = useState("count");

    return (
        <p>{state.count}</p>
    );
}

The setState function can be called to update the state:

function Counter() {
    const state = useState();

    return (
        <div>
            <p>Hello, {state.name}!</p>
            {state.count}
            <button onClick={() => setState({ count: state.count + 1 })}>Increment</button>
        </div>
    );
}

As you can see in the example, the value passed to setState does not need to contain all of the properties in the state object. If you leave one out, it will not be modified.

You can also call setState with no parameters to use mutations to the existing state object:

state.name = "Test";
setState();

Events

Events work similarly to state stores. You can create an event with the createEvent function:

import { createEvent } from 'niue';

const [useOnEvent, emit] = createEvent<string>();

The createEvent function doesn't accept any parameters, however it does have a type parameter for the message type.

The useOnEvent hook can be used in a component to subscribe to the event, and the emit function can be used to send the event:

function EventDemo() {
    useOnEvent((message) => {
        alert(`Hello, ${message}!`);
    }, []);

    return (
        <div>
            Event demo
            <EventEmitter />
        </div>
    )
}

function EventEmitter() {
    return (
        <button onClick={() => emit(prompt("Enter your name"))}>Send event</button>
    );
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].