import {createStore, createEffect, forward} from 'effector'
import {useStore, createGate} from 'effector-react'
const getTodoFx = createEffect(async ({id}) => {
const req = await fetch(`https://jsonplaceholder.typicode.com/todos/${id}`)
return req.json()
})
const $todo = createStore(null).on(getTodoFx.doneData, (_, result) => result)
const TodoGate = createGate()
forward({from: TodoGate.state, to: getTodoFx})
TodoGate.open.watch(() => {
})
TodoGate.close.watch(() => {
})
function Todo() {
const todo = useStore($todo)
const loading = useStore(getTodoFx.pending)
if (loading) {
return <div>Loading...</div>
}
if (Object.keys(todo).length === 0) {
return <div>empty</div>
}
return (
<div>
<p>title: {todo.title}</p>
<p>id: {todo.id}</p>
</div>
)
}
const App = () => {
const [id, setId] = React.useState(0)
return (
<>
<button onClick={() => setId(id + 1)}>Get next Todo</button>
{
}
<TodoGate id={id} />
<Todo />
</>
)
}
ReactDOM.render(<App />, document.getElementById('root'))