1 / 34

React Components Lifecycle | React Tutorial for Beginners | ReactJS Training | Edureka

This Edureka tutorial on React Components will help you in understanding the fundamentals of components in ReactJS. This tutorial helps you learn the following topics:<br><br>1. React Components<br>2. Props<br>3. State<br>4. Flow Of Stateless & Stateful Component<br>5. Lifecycle Of Component

EdurekaIN
Download Presentation

React Components Lifecycle | React Tutorial for Beginners | ReactJS Training | Edureka

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda  What Is Artificial Intelligence ?  What Is Machine Learning ?  Limitations Of Machine Learning  Deep Learning To The Rescue  What Is Deep Learning ?  Deep Learning Applications Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  2. Agenda React 1 1 2 3 Components 4 5 Props State Flow Of Stateless & Stateful Components 4 Life Cycle Of Components 5 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  3. React Components Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  4. React Components In React everything is a component Browser 1 3 2 4 5 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  5. React Components All these components are integrated together to build one application Browser 1 3 2 4 5 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  6. React Components We can easily update or change any of these components without disturbing the rest of the application Browser 1 3 2 4 5 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  7. React Components – UI Tree Single view of UI is divided into logical pieces. The starting component becomes the root and rest components become branches and sub-branches. Browser UI Tree 1 1 3 2 3 2 4 5 4 5 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  8. ReactJS Components – Sample Code Each component returns ONE DOM element, thus JSX elements must be wrapped in an enclosing tag class Component1 extends React.Component{ render() { return( <div> <h2>Hello World</h2> <h1>Welcome To Edureka</h1> </div> ); } } Hello World Welcome To Edureka 3 Enclosing Tag 2 ReactDOM.render( <Component1/>,document.getElementById('content') ); 4 5 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  9. React Components React components are controlled either by Props or States State Prop Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  10. Props Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  11. Props Props help components converse with one another. <Body/> BOB {this.props.name} class Body extends React.Component({ <Header/> render (){ return( <Header name =“BOB”/> <Footer name =“ALEX”/> ); } }); <Footer/> ALEX {this.props.name} Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  12. Props Using Props we can configure the components as well <Body/> MILLER {this.props.name} class Body extends React.Component({ <Header/> render (){ return( <Header name =“MILLER”/> <Footer name =“CODDY”/> ); } }); <Footer/> CODDY {this.props.name} Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  13. Props Props 1 Work similar to HTML attributes Components Data flows downwards from the parent component 2 Props Props Uni-directional data flow 3 Components Components 4 Props are immutable i.e pure Props Props Components Components 5 Can set default props Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  14. Props ES5 Parent Component var Body = React.createClass( { render: function(){ return ( <div> <h1 > Hello World from Edureka!!</h1> <Header name = "Bob"/> <Header name = "Max"/> <Footer name = "Allen"/> Sending Props </div> ); } } ); Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  15. Props ES5 var Header = React.createClass({ render: function () { return( <h2>Head Name: {this.props.name} </h2> ); } }); var Footer = React.createClass({ render: function () { return( <h2>Footer Name: {this.props.name}</h2> ); } }); ReactDOM.render( <Body/>, document.getElementById('container') ); Child Components Receiving Props Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  16. State Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  17. State Components can change, so to keep track of updates over the time we use state State changes because of some event ICE WATER Increase In Temperature Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  18. State Props Components 1 Unlike Props, component States are mutable Props Props 2 Objects which control components rendering and behavior Components Components State State 3 Core of React Components Props Props 4 Must be kept simple Components Components State State Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  19. State Compone nt Component Compone nt Compone nt Stateless Stateful Dumb Components Smart Components Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  20. State Stateless • • Calculates states internal state of components Contains no knowledge of past, current and possible future state changes STATEFUL <Component/> Stateful • Core which stores information about components state in memory Contains knowledge of past, current and possible future state changes STATELESS <Component/> STATELESS <Component/> • Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  21. Flow Of Stateless & Stateful Components Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  22. Flow Of Stateless & Stateful Components ES6 Parent Component class MyApp extends React.Component { constructor(props) { super(props); this.state = { isLoggedIn: false } Setting Initial State } receiveClick() { this.setState({ isLoggedIn: !this.state.isLoggedIn }); } Changing State Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  23. Flow Of Stateless & Stateful Components ES6 render() { return( <div> <h1>Hello.. I am Stateful Parent Component</h1><br/> <p>I monitor if my user is logged in or not!!</p> <br/> <p>Let's see what is your status : <h2><i>{this.state.isLoggedIn ? 'You Are Logged In' : 'Not Logged In'}</i></h2></p><br/> <h2>Hi, I am Stateless Child Component</h2> <p>I am a Logging Button</p><br/> <p><b>I don't maintain state but I tell parent component if user clicks me </b></p><br/> <MyButton click={this.receiveClick.bind(this)} isLoggedIn= {this.state.isLoggedIn}/> </div> ); } Passing Props To Child Component } Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  24. Flow Of Stateless & Stateful Components Child Component ES6 const MyButton = (props) => { return( <div> <button onClick={() => props.click()}> Click TO ---> { props.isLoggedIn ? 'Log Out' : 'Log In'} </button> </div> ); }; Receiving Props From Parent Component ReactDOM.render( <MyApp />, document.getElementById('content') ); Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  25. Flow Of Stateless & Stateful Components State Changes Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  26. Component Lifecycle Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  27. Lifecycle Phases Lifecycle methods notifies when certain stage of the lifecycle occurs 04 01 Code can be added to them to perform various tasks 03 02 Provides a better control over each phase Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  28. Lifecycle Phases 1 In this phase, component is about to make its way to the DOM 04 01 03 02 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  29. Lifecycle Phases 2 In this phase, component can update & re-render when a statechange occurs 04 01 03 02 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  30. Lifecycle Phases 3 In this phase, component can update & re-render when a prop change occurs 04 01 03 02 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  31. Lifecycle Phases removed from DOM 4 In this phase, the component is destroyed and 04 01 03 02 Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  32. Component Lifecycle In A Glance getDefaultProps() ReactDOM.render() getInitialState() componentWillMount() can use this.state render() componentDidMount() setProps() ReactDOM.unmount ComponentAtNode() setStates() componentsWillReceiveProps(nextProps) false componentWillUnmount() forceUpdate() shouldComponentUpdate(nextProps,nextState) true componentWillUpdate(nextProps,nextState) render() componentDidUpdate(prevProps, prevState) Copyright © 2017, edureka and/or its affiliates. All rights reserved.

  33. Popularity Copyright © 2017, edureka and/or its affiliates. All rights reserved.

More Related