Created
February 27, 2019 22:05
-
-
Save 3nvi/dd0c343d182ac3c3b824870cab311241 to your computer and use it in GitHub Desktop.
Minimize anonymous function props
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
// Don't do this! | |
function Component(props) { | |
return <AnotherComponent onChange={() => props.callback(props.id)} /> | |
} | |
// Do this instead :) | |
function Component(props) { | |
const handleChange = useCallback(() => props.callback(props.id), [props.id]); | |
return <AnotherComponent onChange={handleChange} /> | |
} | |
// Or this for class-based components :) | |
class Component extends React.Component { | |
handleChange = () => { | |
this.props.callback(this.props.id) | |
} | |
render() { | |
return <AnotherComponent onChange={this.handleChange} /> | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment