Use with React / Preact
Use with React / Preact
There are 3 ways to use Reim with React, and you can choose depending on scenerios.
0. 'Hook' way (React 16.8+)
import React from 'react'
import reim from 'reim'
import {useReim} from 'react-reim'
const store = reim({count: 8})
function Counter() {
const [state, setState] = useReim(store, /* getter */)
const increment = () => setState(s => {s.count++})
return (
<div>
<button onClick={increment}>+</button>
<div id="count">{state.count}</div>
</div>
)
}1. Minimal way
react-reim 's react plugin adds get function so that you can use derive component from store state
2. Unstated way
react-reim 's react plugin also adds Consumer component so that you can use store right in components
Now to use it:
3. Redux way
The Unstated way is convenient, but sometimes we want to make sure to separate Container Component and Presentation Component.
This is why Reim also provides the connect function
Sync with react-router
Just need to use pipeTo...
Last updated