Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/quiibz/particule

Fine-grained atomic React state management library
https://github.com/quiibz/particule

atom dispatch react recoil redux state state-management

Last synced: 21 days ago
JSON representation

Fine-grained atomic React state management library

Awesome Lists containing this project

README

        


particule









Fine-grained atomic React state management library




yarn add particule

---

- [✨ Features](#-features)
- [🚀 Examples](#-examples)
- [Basic](#basic)
- [Fine-grained](#fine-grained)
- [Composition](#composition)
- [Suspense](#suspense)
- [Dispatch](#dispatch)
- [Custom `atom` with hooks](#custom-atom-with-hooks)
- [📚 Documentation](#-documentation)
- [License](#license)

**Particule** is an atomic React state management library inspired by the best of [Recoil](https://recoiljs.org/), [Jotai](https://jotai.pmnd.rs/) and [Redux](https://redux.js.org/). You can choose **which component subscribe to which state** and so **avoid useless re-render and computations**.

## ✨ Features

- **Super-easy** API
- **TypeScript** ready
- **Suspense** support
- **Minimal** footprint (1kB gzipped)
- **Hooks** to add functionality

## 🚀 Examples

### Basic



```tsx
const textAtom = atom('Hello world!')

function App() {
const [text, setText] = useAtom(textAtom)

return (
<>

{text}


setText('Updated!')}>Update
>
)
}
```

### Fine-grained



```tsx
const textAtom = atom('Hello world!')

function Text() {
const text = useGetAtom(textAtom)

return

{text}


}

// Won't re-render!
function Button() {
const setText = useSetAtom(textAtom)

return setText('Updated!')}>Update
}

// Won't re-render!
function App() {
return (
<>


>
)
}
```

### Composition



```tsx
const eurosAtom = atom(10)
const dollarsAtom = atom(get => get(eurosAtom) * 1.15)

function App() {
const [euros, setEuros] = useAtom(eurosAtom)
const [dollars, setDollars] = useAtom(dollarsAtom)

return (
<>
setEuros(target.value)} value={euros} />
setDollars(target.value)} value={dollars} />
>
)
}
```

### Suspense



```tsx
const nameAtom = atom(async () => {
const json = await (await fetch("https://randomuser.me/api/")).json();

return json.results[0].name.first;
});

function Name() {
const name = useGetAtom(nameAtom)

return

My name is {name}


}

function App() {
return (



)
}
```

### Dispatch



```tsx
const counterAtom = atom(0)
const dispatchCounter = dispatch(counterAtom, value => ({
increment: (newValue: number) => value + newValue,
decrement: (newValue: number) => value - newValue,
}))

function App() {
const counter = useGetAtom(counterAtom)

return (
<>

{counter}


dispatchCounter('increment', 1)}>Increment
dispatchCounter('decrement', 1)}>Decrement
>
)
}
```

### Custom `atom` with hooks



```tsx
const noZeroAtom = createAtom({
beforeValueSet: (_, value) => {
if (value === 0) {
throw new Error('Cannot set value to 0')
}

return value
}
})

const counterAtom = noZeroAtom(3)

function App() {
const [count, setCount] = useAtom(counterAtom)

return (
<>

{count}


setCount(count => count - 1)}>Reduce
>
)
}
```

## 📚 Documentation

See the website at [particule.vercel.app](https://particule.vercel.app/).
Hosted on [Vercel](https://vercel.com/).

## License

[MIT](./LICENSE)