improve performance : shoudComponentUpdate, PureComponent

This commit is contained in:
haerong22
2020-12-22 20:23:13 +09:00
parent ac032f6541
commit aa242cf0fb
2 changed files with 38 additions and 2 deletions

View File

@@ -0,0 +1,35 @@
import React, { PureComponent } from 'react';
class Test extends PureComponent {
state = {
counter: 0,
string: 'hello',
number: 1,
boolean: true,
object: {},
array: [],
};
// shouldComponentUpdate(nextProps, nextState, nextContext) {
// if(this.state.counter !== nextState.counter) {
// return true;
// }
// return false;
// }
onClick = () => {
this.setState({
array: [...this.state.array, 1],
});
}
render() {
console.log('렌더링', this.state);
return (
<div>
<button onClick={this.onClick}>클릭</button>
</div>
)
}
}
export default Test;

View File

@@ -1,6 +1,7 @@
import React from 'react';
import ReactDom from 'react-dom';
import NumberBaseball from './NumberBaseball';
import Test from './RenderTest';
ReactDom.render(<NumberBaseball />, document.querySelector('#root'));
ReactDom.render(<Test />, document.querySelector('#root'));