0.2.10 • Published 12 months ago

min5tore v0.2.10

Weekly downloads
-
License
-
Repository
-
Last release
12 months ago

min5tore

Basic example

import min5tore from 'min5tore';

const [counter, useCounter] = min5tore(1);

// access the store outside React
setInterval(() => {
  counter.set(x => x + 1);
});

const App = () => {
  const { state, set } = useCounter();
  return <h1 onClick={() => set(x => x + 1)}>{state}</h1>;
};

Async value

import min5tore from 'min5tore';

const [, useTodoList] = min5tore([]);
const fetchTodos = () =>
  fetch('https://jsonplaceholder.typicode.com/todos/1').then(x => x.json());

const App = () => {
  const { state, loading, error, set } = useTodoList();
  const load = () =>
    // set function accepts promise object
    // loading prop will be true if promise is pending and false if promise is resolved or rejected
    set(fetchTodos());

  // handle error
  if (error) return <div>{error.message}</div>;

  return (
    <>
      <pre>{JSON.stringify(state)}</pre>
      <button onClick={load}>{loading ? 'Loading...' : 'Load'}</button>
    </>
  );
};

Suspense

import min5tore from 'min5tore';
import { Suspense } from 'react';

const [todoList, useTodoList] = min5tore([]);
const fetchTodos = () =>
  fetch('https://jsonplaceholder.typicode.com/todos/1').then(x => x.json());

const TodoList = () => {
  const [state, { set }] = useTodoList().wait();
  const load = () => set(fetchTodos());

  return (
    <>
      <pre>{JSON.stringify(state)}</pre>
      <button onClick={load}>Load</button>
    </>
  );
};

const App = () => {
  return (
    <Suspense fallback="Loading...">
      <TodoList />
    </Suspense>
  );
};
0.2.10

12 months ago

0.2.9

12 months ago

0.2.8

12 months ago

0.2.7

12 months ago

0.2.6

12 months ago

0.2.5

12 months ago

0.2.4

12 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.1.4

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago