react
play

REACT comp o sabl e component s HELLO WORLD <div - PowerPoint PPT Presentation

CS498RK SPRING 2020 REACT comp o sabl e component s HELLO WORLD <div id="root"></div> ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') ); simples t reac t exampl e


  1. CS498RK SPRING 2020 REACT comp o sabl e component s

  2. HELLO WORLD <div id="root"></div> ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('root') ); simples t reac t exampl e https://reactjs.org/docs/hello-world.html

  3. JSX Syntax extension to JavaScript that produces const element = <h1>Hello, world!</h1>; produces React “elements” Comes with the full const name = 'Deniz Arsan'; const element = <h1>Hello, {name}</h1>; power of JavaScript! https://reactjs.org/docs/introducing-jsx.html

  4. JSX function formatName(user) { return user.firstName + ' ' + user.lastName; } You can put any const user = { valid JavaScript firstName: 'Deniz', lastName: 'Arsan' expression inside }; the curly braces in const element = ( JSX <h1> Hello, {formatName(user)}! </h1> ); https://reactjs.org/docs/introducing-jsx.html

  5. JSX function getGreeting(user) { if (user) { JSX is an return <h1>Hello, {formatName(user)}!</h1>; } expression too return <h1>Hello, Stranger.</h1>; } Can be used to const element = <img src={user.avatarUrl} />; specify attributes https://reactjs.org/docs/introducing-jsx.html

  6. JSX Babel compiles JSX down to const element = ( 1 React.createElement() calls <h1 className="greeting"> Hello, world! </h1> React.createElement() ); 2 creates React elements const element = { type: 'h1', const element = React.createElement( props: { 'h1', className: 'greeting', { className: 'greeting' }, children: 'Hello, world!' 'Hello, world!' } ); }; https://reactjs.org/docs/introducing-jsx.html

  7. RENDERING ELEMENTS let counter = 0; function tick() { const element = ( <div> <h1>You loaded this page {counter} seconds ago.</h1> </div> ); ReactDOM.render(element, document.getElementById('root')); counter = counter + 1; } setInterval(tick, 1000); CodePen <div id="root"></div> https://reactjs.org/docs/rendering-elements.html

  8. COMPONENTS There are two ways to declare a component: function Greeter(props) { with JS functions return <h1>Hello, {props.name}</h1>; } class Greeter extends React.Component { render() { with ES6 Classes return <h1>Hello, {this.props.name}</h1>; } } https://reactjs.org/docs/components-and-props.html

  9. COMPONENTS Here’s how to render a component: Call ReactDOM.render() with 1 function Greeter(props) { <Greeter name="Deniz /> return <h1>Hello, {props.name}</h1>; } React calls Greeter with 2 { name: "Deniz" } const element = <Greeter name="Deniz" />; ReactDOM.render( Greeter returns 3 element, <h1>Hello, Deniz </h1> document.getElementById('root') ); 4 ReactDOM updates the DOM https://reactjs.org/docs/components-and-props.html

  10. COMPONENTS We can use components inside other components too. This is called composing : function Greeter(props) { return <h1>Hello, {props.name}</h1>; } function App() { return ( ReactDOM.render( <div> <App />, <Greeter name="Deniz" /> document.getElementById('root') <Greeter name="Jinda" /> ); <Greeter name="Wanxian" /> </div> ); } https://reactjs.org/docs/components-and-props.html

  11. COMPONENTS IMPORTANT RULE All React components must act like pure functions with respect to their props. pur e impur e function sum(a, b) { function withdraw(account, amount) { VS return a + b; account.total -= amount; } } https://reactjs.org/docs/components-and-props.html

  12. STATE let counter = 0; function tick() { const element = ( <div> <h1>You loaded this page {counter} seconds ago.</h1> </div> ); ReactDOM.render(element, document.getElementById('root')); counter = counter + 1; } setInterval(tick, 1000); We want to make this Timer reusable and encapsulated . It needs set up its own timer and update itself . https://reactjs.org/docs/state-and-lifecycle.html

  13. STATE 1 Create a stateful component that keeps track of its state class Timer extends React.Component { constructor(props) { super(props); this.state = { counter: 0 }; } render() { return ( <h1> You loaded this page {this.state.counter} seconds ago. </h1> ); } } https://reactjs.org/docs/state-and-lifecycle.html

  14. STATE 2 Use lifecycle methods to change the state componentDidMount() { this.timerID = setInterval( () => this.tick(), 1000 tick() { ); this.setState((state, props) => ({ } counter: state.counter + 1 })); componentWillUnmount() { } clearInterval(this.timerID); } 
 https://reactjs.org/docs/state-and-lifecycle.html

  15. LIFECYCLE METHODS componentDidUpdate() componentWillUnmount() componentDidMount() Invoked immediately after a component is Invoked immediately after Invoked immediately before a inserted into the tree. updating occurs. component is unmounted and destroyed. Initialization that requires This method is not called DOM nodes should go for the initial render. Any cleanup should go here here https://reactjs.org/docs/react-component.html

  16. COMPONENT LIFECYCLE

  17. setState() Used to update the state Do not update the state directly 1 this.state.name = 'Deniz'; // Wrong this.setState({ name: 'Deniz' }); // Correct 2 Updates may be asynchronous this.setState({ counter: this.state.counter + 1 }); // Wrong this.setState((state, props) => ({ counter: state.counter + 1})); // Correct 3 Updates are merged this.state = { name: '', title: '' }; this.setState({ name: 'Deniz' }); https://reactjs.org/docs/state-and-lifecycle.html

  18. Time r Componen t wit h Stat e CodePen

  19. EVENTS Create handler in the component 1 handleClick() { this.setState(state => ({ isActive: !state.isActive })); } 2 Assign handler to event in the element <button onClick={this.handleClick}> 3 Make this refer to the component CodePen this.handleClick = this.handleClick.bind(this); https://reactjs.org/docs/handling-events.html

  20. EVENTS Resolving this Bind it in the constructor this.handleClick = this.handleClick.bind(this); OR Use arrow functions when assigning <button onClick={(e) => this.handleClick(e)}> OR handleClick = () => {...} Use class fields syntax when declaring https://reactjs.org/docs/handling-events.html

  21. EVENTS Passing Arguments arro w function s <button onClick={(e) => this.handleClick(id, e)}>Go</button> bin d i n elemen t <button onClick={this.handleClick.bind(this, id)}>Go</button> https://reactjs.org/docs/handling-events.html

  22. RENDERING LISTS .ma p i n j s x function List(props) { const { numbers } = props; function Item(props) { return ( return <li>{props.value}</li>; <ul> } {numbers.map((number) => <Item const numbers = [1, 2, 3, 4, 5]; key={number.toString()} value={number} ReactDOM.render( /> <List numbers={numbers} />, )} document.getElementById('root') </ul> ); ); } uniqu e ke ys CodePen https://reactjs.org/docs/lists-and-keys.html

  23. CONTROLLED COMPONENTS <input> , <textarea> , and <select> maintain their own state handleChange(event) { this.setState({ value: event.target.value }); React components keep their own state } and update it with setState() <input type="text" value={this.state.value} Controlled components allow us to onChange={this.handleChange} /> have a single source of truth - React controls the value of a form element CodePen https://reactjs.org/docs/forms.html

  24. dem o https://gitlab.com/uiuc-web- programming/react-demo

  25. RESOURCES Step-by-step guide https://reactjs.org/docs/hello-world.html Learn-by-doing Guide https://reactjs.org/tutorial/tutorial.html

  26. NEXT CLASS: REACT STATE/ROUTE MANAGEMENT https://uiuc-web-programming.gitlab.io/sp20/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend