Skip to content

Instantly share code, notes, and snippets.

@BenBrostoff
Created August 11, 2019 12:28
Show Gist options
  • Save BenBrostoff/c652ac47b58bc3402d80887459f9ba26 to your computer and use it in GitHub Desktop.
Save BenBrostoff/c652ac47b58bc3402d80887459f9ba26 to your computer and use it in GitHub Desktop.
Simply async act
export const AsyncApp = () => {
const [data, setData] = useState('unloaded value');
const [clickCount, setCount] = useState(0);
const simulatedFetch = async () => {
const fetchedValue = await Promise.resolve('fetched value');
await new Promise(res => {
setData(fetchedValue);
res();
});
};
useEffect(() => {
simulatedFetch();
}, []);
const onClick = async () => {
await Promise.resolve();
setData('clicked');
setCount(clickCount + 1);
}
return (
<div
style={styles}
data-testid="click"
onClick={onClick}>
<h2>{data}</h2>
<h3>{clickCount} clicks</h3>
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment