Last active
July 30, 2021 02:24
-
-
Save jacob-beltran/7777a477942cbb2c9db65a1e3c312e88 to your computer and use it in GitHub Desktop.
React Performance: Object Literals Example
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
/* | |
Object literals or Array literals are functionally equivalent to calling | |
Object.create() or new Array(). This means that if object literals or | |
array literals are passed as prop values, React will consider these to be new | |
values for each render. | |
This is problematic mostly when dealing with Radium or inline styles. | |
*/ | |
/* Bad */ | |
// New object literal for style each render | |
render() { | |
return <div style={ { backgroundColor: 'red' } }/> | |
} | |
/* Good */ | |
// Declare outside of component | |
const style = { backgroundColor: 'red' }; | |
render() { | |
return <div style={ style }/> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment