0.1.2 • Published 6 months ago

ryze v0.1.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 months ago

Ryze

A minimal state management library for React.

Install

npm install ryze

Example

import {createStore} from 'ryze';

const {store, useSlice} = createStore({count: 10, todos: []});

const Counter = () => {
  const count = useSlice('count');

  return (
    <div>
      <div>count: {count}</div>
      <button
        onClick={() => {
          store.setState((prev) => ({...prev, count: prev.count + 1}));
        }}
      >
        Add
      </button>
    </div>
  );
};

const getActiveTodos = (state) => state.todos.filter((item) => !item.completed);

const Todos = () => {
  const todos = useSlice(getActiveTodos);

  return (
    <div>
      <ul>
        {todos.map((todo) => (
          <li key={todo.date}>{todo.title}</li>
        ))}
      </ul>
      <button
        onClick={() => {
          store.setState((prev) => ({
            ...prev,
            todos: [...prev.todos, {title: 'New Todo', date: +new Date()}],
          }));
        }}
      >
        Add
      </button>
    </div>
  );
};

const Example = () => {
  return (
    <>
      <Counter />
      <Todos />
    </>
  );
};

Constraints

Selectors

Selector passed to useSlice should have the same identity across re renders.

That is,

  • either declare selectors outside of components, or
  • if the selector is dependent on component props, use useCallback to ensure the selector only changes when the prop change.

Updates

Updates should be immutable. Return new values, rather than modify the state value directly.

0.1.2

6 months ago

0.1.0

11 months ago

0.1.1

10 months ago

0.0.10

12 months ago

0.0.11

12 months ago

0.0.12

12 months ago

0.0.1

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.9

12 months ago

0.0.8

12 months ago

0.0.5

12 months ago

0.0.4

12 months ago

0.0.7

12 months ago

0.0.6

12 months ago

0.0.0

4 years ago