When using PureComponents you have the advantage over functional components that the component is not always rendered when the parent updates. It actually only renders when there is a change in the component props, which in this example is only when you change the input.
How could you pass an object to the PureComponent without it breaking all the advantage of using this kind of component?
Does this mean that as soon as you have a prop that you expect it to be of type object, it's better to just make your component functional?
I added an example to illustrate what I mean... (Probably you need to open it in a new window, as there are a lot of different children)
class TestPureComponent extends React.PureComponent {
render() {
return <div style={{border: '1px solid black'}}>{this.props.text} : {this.props.inputValue} {Math.random()} <button onClick={() => {this.props.dismissClicked()}}>Click me</button> </div>
}
}
function TestFunctionalComponent () {
return <div style={{border: '1px solid black'}}>I always update as I am a functional component {Math.random()}</div>
}
const hell = () => {console.log('Logs hello')}
class RenderExample extends React.Component {
constructor (props) {
super(props)
this.state = {clicked: false, inputValue: 'inputValue'}
this.onClick = this.onClick.bind(this)
this.doSomething = this.doSomething.bind(this)
}
onClick () {
this.setState({clicked: !this.state.clicked})
}
doSomething () {
console.log('helllllo')
}
heee = () => {
console.log('heeeee')
}
render () {
return <div>
<button onClick={this.onClick}>
Update state (forces re-render) {this.state.clicked && 'clicked'}
</button>
<input onChange={(e) => this.setState({inputValue: e.target.value})} type="text" value={this.state.inputValue}/>
<br/>
<br/>
<TestFunctionalComponent />
<br/>
<TestPureComponent dismissClicked={() => hell} inputValue={this.state.inputValue} text="If you click the button this will re-render, if you change the input this will re-render"/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={this.doSomething} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={this.heee} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={hell} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will NOT re-render, if you change the input this will re-render" dismissClicked={hell} inputValue={this.state.inputValue}/>
<br/><br/>
<div> we will now add an inline object to each component and now they all update</div>
<TestPureComponent dismissClicked={() => hell} inlineOBJ={{hello: 'world'}} inputValue={this.state.inputValue} text="If you click the button this will re-render, if you change the input this will re-render"/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={this.doSomething} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={this.heee} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={hell} inputValue={this.state.inputValue}/>
<br/>
<TestPureComponent text="If you click the button this will re-render, if you change the input this will re-render" inlineOBJ={{hello: 'world'}} dismissClicked={hell} inputValue={this.state.inputValue}/>
</div>
}
}
ReactDOM.render(<RenderExample />, document.getElementById('root'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
What I want is to see if there is a way to have a PureComponent, that wouldn't ALWAYS update when there is an OBJECT passed in as a prop.