Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hastebrot
Last active March 29, 2019 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hastebrot/af567b8c86ac5af039a574b4cf67b199 to your computer and use it in GitHub Desktop.
Save hastebrot/af567b8c86ac5af039a574b4cf67b199 to your computer and use it in GitHub Desktop.
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