Guest User

Untitled

a guest
Jul 15th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. class OnlyEvens extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. }
  5. shouldComponentUpdate(nextProps, nextState) {
  6. console.log('Should I update?');
  7. // change code below this line
  8. return (nextProps.value % 2 === 0); // check if valueof next prop is even. If it is, it renders, else it does not render.
  9. // change code above this line
  10. }
  11. componentWillReceiveProps(nextProps) {
  12. console.log('Receiving new props...');
  13. }
  14. componentDidUpdate() {
  15. console.log('Component re-rendered.');
  16. }
  17. render() {
  18. return <h1>{this.props.value}</h1>
  19. }
  20. };
  21.  
  22. class Controller extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. value: 0
  27. };
  28. this.addValue = this.addValue.bind(this);
  29. }
  30. addValue() {
  31. this.setState({
  32. value: this.state.value + 1
  33. });
  34. }
  35. render() {
  36. return (
  37. <div>
  38. <button onClick={this.addValue}>Add</button>
  39. <OnlyEvens value={this.state.value}/>
  40. </div>
  41. );
  42. }
  43. };
Add Comment
Please, Sign In to add comment