Skip to content

Instantly share code, notes, and snippets.

@vicentedealencar
Forked from steida/HelloMessage.js
Last active June 23, 2016 04:47
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 vicentedealencar/1db2525458de27792baf85d9bba24a07 to your computer and use it in GitHub Desktop.
Save vicentedealencar/1db2525458de27792baf85d9bba24a07 to your computer and use it in GitHub Desktop.
React stateless components with lifecycle proposal
const HelloMessage = props => <div>Hello {props.name}</div>
HelloMessage.intialState = { isMounted: false }
const focusAfterAfterOneSec = el => {
setTimeout(() => {
if (!isMounted) return;
el.focus();
}, 1000);
}
HelloMessage.componentDidMount = componentInstance => {
componentInstance.setState({ isMounted: true})
const el = ReactDOM.findDOMNode(componentInstance)
focusAfterAfterOneSec(el)
}
HelloMessage.componentWillUnmount = componentInstance => {
componentInstance.setState({ isMounted: false})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment