Guest User

Untitled

a guest
Mar 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. class Children extends Component {
  2. constructor(props) {
  3. console.log('CHILDREN - constructor', props);
  4. super(props);
  5. this.state = {
  6. message: '',
  7. }
  8. this.onClick = this.onClick.bind(this)
  9. }
  10.  
  11.  
  12. componentWillMount() {
  13. console.log('CHILDREN - componentWillMount()');
  14. }
  15.  
  16. componentDidMount() {
  17. console.log('CHILDREN - componentDidMount()');
  18.  
  19. }
  20. componentWillUnmount() {
  21. console.log('CHILDREN - componentWillUnmount()');
  22. }
  23. componentWillReceiveProps(nextProps) {
  24. console.log('CHILDREN - componentWillReceiveProps(nextProps)', nextProps);
  25.  
  26. }
  27. shouldComponentUpdate(nextProps, nextState) {
  28. console.log('CHILDREN - shouldComponentUpdate(nextProps, nextState)');
  29. return true
  30. }
  31. componentWillUpdate(nextProps, nextState) {
  32. console.log('CHILDREN - componentWillUpdate(nextProps, nextState)');
  33.  
  34. }
  35. componentDidUpdate(prevProps, prevState) {
  36. console.log('CHILDREN - componentDidUpdate(prevProps, prevState)');
  37.  
  38. }
  39.  
  40. onClick() {
  41. console.log('onClick');
  42. this.setState({
  43. message: 'state updated in children',
  44. });
  45. }
  46.  
  47. render() {
  48. console.log('CHILDREN - render()');
  49.  
  50. return (
  51. <div>
  52. <h3>Children</h3>
  53. <h5> props: {this.props.message} </h5>
  54. <h5> state: {this.state.message} </h5>
  55. <button onClick={this.onClick}> Modify State in children</button>
  56. </div>
  57. );
  58. }
  59. }
  60.  
  61. class Parent extends Component {
  62. constructor(props) {
  63. console.log('PARENT - constructor', props);
  64. super(props);
  65. this.state = {
  66. message: ''
  67. }
  68. this.something = ''
  69.  
  70. }
  71.  
  72.  
  73. componentWillMount() {
  74. console.log('PARENT - componentWillMount()');
  75. }
  76.  
  77. componentDidMount() {
  78. console.log('PARENT - componentDidMount()');
  79.  
  80. }
  81. componentWillUnmount() {
  82. console.log('PARENT - componentWillUnmount()');
  83. }
  84. componentWillReceiveProps(nextProps) {
  85. console.log('PARENT - componentWillReceiveProps(nextProps)');
  86. }
  87. shouldComponentUpdate(nextProps, nextState) {
  88. console.log('PARENT - shouldComponentUpdate(nextProps, nextState)');
  89. return true; // we are forcing to update.
  90. }
  91. componentWillUpdate(nextProps, nextState) {
  92. console.log('PARENT - componentWillUpdate(nextProps, nextState)');
  93.  
  94. }
  95. componentDidUpdate(prevProps, prevState) {
  96. console.log('PARENT - componentDidUpdate(prevProps, prevState)');
  97.  
  98. }
  99.  
  100. onClick = () => {
  101. this.something = 'something'
  102. this.setState({
  103. message: 'Change State in Parent'
  104. });
  105. this.forceUpdate();
  106. }
  107.  
  108. render() {
  109. console.log('PARENT - Render');
  110.  
  111. return (
  112. <div>
  113. <h3>Parent</h3>
  114. {this.state.message}
  115. <button onClick={this.onClick}>Change State in Parent</button>
  116. <Children message={this.state.message} />
  117. </div>
  118. )
  119. }
  120. }
  121.  
  122. ReactDOM.render(<Parent />, document.getElementById('root'));
Add Comment
Please, Sign In to add comment