Skip to content

Instantly share code, notes, and snippets.

@noahlt
Created October 17, 2015 00:52
Show Gist options
  • Save noahlt/b556788956ef0700a413 to your computer and use it in GitHub Desktop.
Save noahlt/b556788956ef0700a413 to your computer and use it in GitHub Desktop.
Hack to block re-rendering child when parent was updated
<!DOCTYPE html>
<html>
<head>
<title>Hack to block re-rendering child when parent was updated</title>
<script src="https://fb.me/react-0.13.0.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.0.js"></script>
</head>
<body>
</body>
<script type="text/jsx">
var App = React.createClass({
getInitialState: function() {
return { foo: false };
},
handleClick: function() {
this.setState({ foo: !this.state.foo });
},
render: function() {
console.log('Rendering App');
return <div style={this.state.foo ? { backgroundColor: '#ddd' } : {backgroundColor: '#fff'}}>
<button onClick={this.handleClick}>Click me</button>
<p>Lorem ipsum dolor sit amet.</p>
<BlockUpdating><Widget color="blue" text="hello"/></BlockUpdating>
<BlockUpdating><Widget color="red" text="goodbye"/></BlockUpdating>
</div>;
}
});
var Widget = React.createClass({
render: function() {
console.log('Rendering Widget', this.props.text);
return <div style={{backgroundColor: this.props.color}}>this.props.text</div>;
}
});
var BlockUpdating = React.createClass({
shouldComponentUpdate: function() {
return false;
},
render: function() {
return <div>{this.props.children}</div>;
}
});
React.render(<App />, document.body);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment