Last active
March 29, 2019 00:57
-
-
Save hastebrot/af567b8c86ac5af039a574b4cf67b199 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, { Fragment, useState, useEffect } from "react" | |
// this is similar to react's render(). | |
export default () => { | |
const [state, setState] = useState({ count: 1 }) | |
// this is similar to react's componentDidMount(). | |
useEffect(() => { | |
setState({ count: 2 }) | |
timeout(500, () => { | |
setState({ count: 3 }) | |
}) | |
}, []) | |
// this is similar to redux's store.subscribe(() => store.getState()). | |
useEffect(() => { | |
console.log("state changed:", state) | |
}, [state]) | |
return <Fragment>{state.count}</Fragment> | |
} | |
const timeout = (durationMillis, action) => { | |
return setTimeout(action, durationMillis) | |
} | |
// OUTPUTS: | |
// state changed: Object { count: 1 } | |
// state changed: Object { count: 2 } | |
// state changed: Object { count: 3 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment