Guest User

Untitled

a guest
Jul 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import React, {Component};
  2.  
  3.  
  4. class ParentComponent extends Component {
  5. //Initial State
  6. state = { data: [] }
  7.  
  8. //Function to update the state
  9. //NOTE-1: You only can use this.setState to update the state
  10. //NOTE-2: You never want to update your state directly that why I use .concat instead of .push.
  11. addElementIntoState = (element) => {
  12. this.setState({data: this.state.data.concat(element)})
  13. }
  14. render(){
  15. return (
  16. // Passing the callback function via props into child
  17. // Each time the child executes this function the parent will know that its state is being updated.
  18. // And will re-render with the new state.
  19. <MyChild addElementIntoState={addElementIntoState}>
  20. )
  21. }
  22. }
Add Comment
Please, Sign In to add comment