Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pyrossh/atoms-state

Simple State management for react
https://github.com/pyrossh/atoms-state

Last synced: 5 days ago
JSON representation

Simple State management for react

Awesome Lists containing this project

README

        

# atoms-state

State management and common hooks

## Usage

```js
import React from 'react';
import ReactDOM from 'react-dom';
import { atom, useAtom } from 'atoms-state';

const countAtom = atom(10);
const sumAtom = atom((get) => get(countAtom) + 10);

const increment = () => {
countAtom.update((count) => count + 1);
};

const decrement = () => {
countAtom.update((count) => count - 1);
};

const Counter = () => {
const count = useAtom(countAtom);
const sum = useAtom(sumAtom);

return (


Count: {count}


Sum: {sum}


Inc
Dec

);
};

ReactDOM.render(, document.getElementById('root'));
```

## Async

```js
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import { asyncAtom, useAsyncAtom } from 'atoms-state';

const todoAtom = asyncAtom(async ({ id }) => {
const res = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`);
return await res.json();
});

const completeTodo = () => {
todoAtom.update((todo) => ({ ...todo, completed: !todo.completed }));
};

const Counter = () => {
const todo = useAsyncAtom(todoAtom, { id: 1 });

return (


id: {todo.id}


userId: {todo.userId}


title: {todo.title}


completed: {todo.completed}


Toggle Complete

);
};

ReactDOM.render(
Loading}>

,
document.getElementById('root'),
);
```