deno.land / x / jotai@v1.8.4 / docs / integrations / redux.mdx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
---title: Reduxdescription: This doc describes `jotai/redux` bundle.nav: 4.07---
Jotai's state resides in React, but sometimes it would be niceto interact with the world outside React.
Redux provides a store interface that can be used to store some valuesand sync with atoms in Jotai.
## Install
You have to install `redux` to access this bundle and its functions.
```npm install redux# oryarn add redux```
## atomWithStore
`atomWithStore` creates a new atom with redux store.It's two-way binding and you can change the value from both ends.
```jsximport { useAtom } from 'jotai'import { atomWithStore } from 'jotai/redux'import { createStore } from 'redux'
const initialState = { count: 0 }const reducer = (state = initialState, action: { type: 'INC' }) => { if (action.type === 'INC') { return { ...state, count: state.count + 1 } } return state}const store = createStore(reducer)const storeAtom = atomWithStore(store)
const Counter = () => { const [state, dispatch] = useAtom(storeAtom) return ( <> count: {state.count} <button onClick={() => dispatch({ type: 'INC' })}>button</button> </> )}```
### Examples
<CodeSandbox id="cmlu5" />
jotai

Version Info

Tagged at
a year ago