Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React JS intro

React JS intro

A lightweight introduction to React JS in the context of web applications

Avatar for Gabriele Petronella

Gabriele Petronella

September 30, 2015
Tweet

More Decks by Gabriele Petronella

Other Decks in Programming

Transcript

  1. I just had to take the hypertext idea and connect

    it to the TCP and DNS ideas and — ta- da!— the World Wide Web — Tim Berners Lee
  2. THE WEB, IN THE 90S ╔═══════════╗ ---------- ║ ║ gimme

    dat page   ║ ║---------------------->  ║ browser ║  server  ║ ║ <html>...</html>   ║ ║<----------------------  ╚═══════════╝ ----------
  3. ╔═══════════╗  ║ ║ gimme dat page   ║

    ║---------------------->  ║ ║   ║ ║ <html>...</html>   ║ ║<----------------------  ║ browser ║ ...  server  ║ ║   ║ ║ do dat thing   ║ ║---------------------->  ║ ║   ║ ║ <html>...</html>   ║ ║<----------------------  ║ ║   ╚═══════════╝ 
  4. ADDING AN ELEMENT TO A CART <ul> <ul> <li>An apple</li>

    <li>An apple</li> <li>A horse</li> ---> <li>A horse</li> </ul> <li>A dragon</li> </ul>
  5. ADDING AN ELEMENT TO A CART var ul = document.getElementById("cart");

    var li = document.createElement("li"); li.appendChild(document.createTextNode("Four")); ul.appendChild(li);
  6. ╔═══════════╗  ║ ║ gimme dat page   ║

    ║---------------------->  ║ ║ <html>...</html>   ║ browser ║<----------------------  ║ ║ script.js   ║ ║<---------------------- server  ║ ║─────┐   ╚═══════════╝ !   ▲ !   <html>...</html>! !add item   ! !   "########$ !   ! ! !  ! script !----- ! ! %########&
  7. ╔═══════════╗  ║ ║ gimme dat page   ║

    ║---------------------->  ║ ║ <html>...</html>   ║ browser ║<----------------------  ║ ║ app.js   ║ ║<----------------------  ║ ╠─────┐   ╚═══════════╝ !   ▲ !  server  <html>...</html>! !do stuff   ! !   "##########$ !   ! !<-----   ! ! gimme data   ! app.js !---------------------->  ! !   ! ! { data: "blah", ... }   ! !<----------------------  %##########& 
  8. MVC

  9. PROPS ! A generalization over HTML attributes <button className='button inactive'>Click</button>

    gets translated to React.createElement( "button", { className: 'button inactive' }, // <--- props "Click" );
  10. OUR FIRST COMPONENT ACCEPTING PROPS const Greeter = React.createClass({ render()

    { return <div>Hello {this.props.name}!</div>; } ^ }); | just a javascript variable
  11. REACT COMPUTES renderA: <div><span>Hello Gabriele!</span></div> renderB: <div><span>Hello Irina!</span></div> => [replaceAttribute

    textContent 'Hello Irina'] ^ | | a state mutation, i.e. the horrible thing you want to avoid writing by hand
  12. const Counter = React.createClass({ getInitialState() { return { count: 0

    }; }, increaseCount() { this.setState({ count: this.state.count + 1 }); }, ^ |_______________ triggers a re-render render() { return ( <div> <span>{this.state.count}</span> <button onClick={this.increaseCount}>Increase</button> </div> ); } });