Created
August 7, 2020 11:35
-
-
Save defint/3faffb974ab76b1f7aa8adf0ff2ebadb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {useEffect} from 'react'; | |
import {declareAction, declareAtom} from "@reatom/core"; | |
import {useAction, useAtom} from "@reatom/react"; | |
const changeAtom = declareAction(); | |
const atomPrimitive = declareAtom(0, on => [ | |
on(changeAtom, (state, payload) => { | |
console.log("new payload: ", payload); | |
return payload; | |
}) | |
]); | |
export const App = () => { | |
const changeValue = useAction(changeAtom); | |
const value = useAtom(atomPrimitive); | |
useEffect(() => { | |
setInterval(() => { | |
changeValue(Math.random()); | |
}, 1000); | |
}, []); | |
return ( | |
<div> | |
<div>{value}</div> | |
<button onClick={() => { | |
changeValue(Math.random()); | |
}}>Update manual</button> | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment