Created
February 13, 2020 15:41
-
-
Save Neugierdsnase/711c629f6063c13d57e4094df496a0cd to your computer and use it in GitHub Desktop.
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
// Taken from here: https://www.reddit.com/r/reactjs/comments/f37x69/what_are_not_popular_but_great_react_tricks | |
// If you want to optimize some of your subrender methods, you can do the trick: | |
import React from 'react'; | |
class Parent extends React.PureComponent { | |
render () { | |
const Table = this.renderTable; | |
return ( | |
<div style={{color: this.props.color}}> | |
Multiplication table: | |
<Table maxX={1000} maxY={1000} /> | |
</div> | |
); | |
} | |
renderTable = React.memo(({maxX, maxY}) => { | |
const rows = []; | |
for (let y = 1; y <= maxY; y++) { | |
const columns = []; | |
for (let x = 1; x <= maxX; x++) { | |
column.push(( | |
<td>{x * y}</td> | |
)); | |
} | |
rows.push(( | |
<tr>{columns}</tr> | |
)); | |
} | |
return ( | |
<table>{rows}</table> | |
); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment