Last active
February 22, 2017 16:21
-
-
Save thaenor/5b4e48c26b8c35a9719168fdf17982df to your computer and use it in GitHub Desktop.
React Notes - because I'm tired of making pens
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
//If a child component wants to communicate back to its parent, it can do so through props, | |
//most commonly by its parent providing a callback property that the child can call when some event happens: | |
const ParentComponent = () => { | |
const letMeKnowAboutSomeThing = () => console.log('something happened!') | |
return ( | |
<ChildComponent letMeKnowAboutSomeThing={letMeKnowAboutSomeThing} /> | |
) | |
} | |
const ChildComponent = props => { | |
const onClick = e => { | |
e.preventDefault() | |
props.letMeKnowAboutSomeThing() | |
} | |
return <a onClick={onClick}>Click me!</a> | |
} |
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
//shouldComponentUpdate example from http://lucybain.com/blog/2017/react-js-when-to-rerender/?utm_source=reactnl&utm_medium=email | |
class Todo extends React.Component { | |
shouldComponentUpdate(nextProps) { | |
const differentTitle = this.props.title !== nextProps.title; | |
const differentDone = this.props.done !== nextProps.done | |
return differentTitle || differentDone; | |
} | |
render() { | |
return (<div>...</div>); | |
} | |
} |
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
//https://discuss.reactjs.org/t/what-are-the-hidden-features-of-react-that-you-know-of/4292/3 | |
this.setState({ someVar: someVal }, () => { | |
// code that depends on this new state | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment