Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. class Parent extends Component {
  2. constructor(props){
  3. super(props);
  4. this.state = {passedValue: this.props.passedFromStaticView};
  5. this.handleParentStateUpdate = this.handleParentStateUpdate.bind(this);
  6. }
  7. // This method is passed as a prop to the child component
  8. handleParentStateUpdate(e){
  9. console.log("handle state update for the parent!")
  10. // Sets the state of this class's passedValue variable
  11. this.setState({passedValue: e})
  12. }
  13. render () {
  14. return (
  15. <React.Fragment>
  16. <Child passedValue={this.state.passedValue} handleParentStateUpdate={this.handleParentStateUpdate} />
  17. </React.Fragment>
  18. );
  19. }
  20. }
  21.  
  22. class Child extends Component {
  23. constructor(props){
  24. super(props);
  25. this.state = {passedValue: this.props.passedValue}
  26. this.handleChildStateUpdate = this.handleChildStateUpdate.bind(this);
  27. }
  28. handleChildStateUpdate(e){
  29. // Update the state of the Child Component
  30. this.setState({passedValue: e})
  31.  
  32. // Calling the method that was passed down from the `Parent` class as a prop
  33. this.props.handleParentStateUpdate(e)
  34. }
  35. render(){
  36. return(
  37. <React.Fragment>
  38. <Child passedValueFromChild={this.state.passedValue} handleParentStateUpdate={this.handleChildStateUpdate} />
  39. </React.Fragment>
  40. )
  41. }
  42. }
  43.  
  44. class GrandChild extends Component {
  45. constructor(props){
  46. super(props);
  47. this.state = {passedValue: this.props.passedValueFromChild}
  48. this.handleChildStateUpdate = this.handleChildStateUpdate.bind(this);
  49. }
  50. handleChildStateUpdate(e){
  51. // Update the state of the GrandChild Component
  52. this.setState({passedValue: e})
  53.  
  54. // Calling the method that was passed down from the `Child` class as a prop
  55. this.props.handleParentStateUpdate(e)
  56. }
  57. render(){
  58. return(
  59. <React.Fragment>
  60. <button onClick={(e) => this.handleChildStateUpdate("State")} className="btn btn-warning">
  61. Update State
  62. </button>
  63. </React.Fragment>
  64. )
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement