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

Understanding React

Understanding React

Raphael Amorim

June 08, 2018
Tweet

More Decks by Raphael Amorim

Other Decks in Technology

Transcript

  1. Software Developer Engineer @godaddy JS Foundation Member @jquery Maintainer React-TV

    Raphael Amorim Previously @globocom, @petrobras 22 years old Mozillian Metido a besta
  2. When the program is running, you may have several instances

    of this component on screen, each with its own properties and local state.
  3. In this traditional UI model, it is up to you

    take care of creating and destroying child component instances.
  4. If a Form component wants to render a Button component.

    It needs to create its instance, and manually keep it up to date with any new information.
  5. class Form extends TraditionalObjectOrientedView { render() { // Read some

    data passed to the view const { isSubmitted, buttonText } = this.attrs; if (!isSubmitted && !this.button) { // Form is not yet submitted. Create the button! this.button = new Button({ children: buttonText, color: 'blue' }); this.el.appendChild(this.button.el); } if (this.button) { // The button is visible. Update its text! this.button.attrs.children = buttonText; this.button.render(); } if (isSubmitted && this.button) { // Form was submitted. Destroy the button! this.el.removeChild(this.button.el); this.button.destroy(); } . . .
  6. Each component has to keep references to its DOM node

    and to the instances of the children components, and create, update, and destroy them when the time is right.
  7. An element is a plain object describing a component instance

    or DOM node and its desired properties.
  8. It contains only information about the component type (for example,

    a Button), its properties (for example, its color), and any child elements inside it.
  9. An element is not an actual instance. Rather, it is

    a way to tell React: What you want to see on the screen
  10. You can’t call any methods on the element. It’s just

    an immutable description object with two fields: type: (string | Component) props: Object.*
  11. You can’t call any methods on the element. It’s just

    an immutable description object with two fields: type: (string | Component) props: Object.*
  12. When the element’s type is a string, an element represents

    a DOM node with that tag name, and props correspond to its attributes. This is what React will render. For example:
  13. By convention, when we want to create an element tree,

    we specify one or more child elements as the children prop of their containing element.
  14. What’s important is that both child and parent elements are

    just descriptions and not actual instances
  15. They don’t refer to anything on the screen when you

    create them. You can create them and throw them away, and it won’t matter much. really… React-DOM, React-Hardware, React-Native, React-TV […] does it.
  16. React Elements are easy to traverse, don’t need to be

    parsed, and of course are much lighter than the actual DOM elements.
  17. However, the type of an element can also be a

    function or class corresponding to a React component.
  18. An element describing a component is also an element, just

    like an element describing the DOM node. They can be nested and mixed with each other.
  19. This feature lets you define a OkButton component as a

    Button with a specific color property value. Without worrying about whether Button renders to a button, a div, or something else entirely.
  20. const ConfirmButton = ({ children }) => ({ type: Button,

    props: { color: ‘green', children: children } });
  21. const CreateAccount = () => ({ type: 'div', props: {

    children: [{ type: 'p', props: { children: ‘Welcome to Nubank!’ } }, { type: ConfirmButton, props: { children: ‘Yahooo!' } }, { type: Button, props: { color: 'red', children: 'Cancel' } }] });
  22. const CreateAccount = () => ( <div> <p>Welcome to Nubank</p>

    <ConfirmButton>Yahooo!</ConfirmButton> <Button color='blue'>Cancel</Button> </div> );
  23. When React sees an element with a function or class

    type, it will know to ask that component what element it renders to with the given props.
  24. React will repeat this process until it knows the underlying

    DOM tag elements for every component on the page.
  25. const Form = ({ isSubmitted, buttonText }) => { if

    (isSubmitted) { // Form submitted! Return a message element. return { type: Message, props: { text: 'Success!' } }; } // Form still visible! Return a button element. return { type: Button, props: { children: buttonText, color: 'blue' } }; };
  26. The returned element tree can contain both elements describing DOM

    nodes, and elements describing other components. This lets you compose independent parts of UI without relying on their internal DOM structure.
  27. We describe them with elements we return from the components,

    and React takes care of managing the instances.
  28. In the last code, Form, Message, and Button are React

    components. They can either be written as functions, as above, or as classes descending from React.Component:
  29. class Button extends React.Component { render() { const { children,

    color } = this.props; // Return an element describing a // <button><b>{children}</b></button> return { type: 'button', props: { className: 'button button-' + color, children: { type: 'b', props: { children: children } } } }; } }
  30. When a component is defined as a class, it is

    a little more powerful than a functional component. It can store some local state and perform custom logic when the corresponding DOM node is created or destroyed.
  31. A functional component is less powerful but is simpler, and

    it acts like a class component with just a single render() method.
  32. However, whether functions or classes, fundamentally they are all components

    to React. They take the props as their input, and return the elements as their output.
  33. At the end of this process React knows the result

    DOM tree, and a renderer like ReactDOM or React Native applies the minimal set of changes necessary to update the actual DOM nodes.
  34. React provides a declarative API so that you don’t have

    to worry about exactly what changes on every update.
  35. Tl;dr;
 
 React’s “diffing” algorithm so that component updates are

    predictable while being fast enough for high- performance apps.
  36. 2. The developer can hint at which child elements may

    be stable across different renders with a key prop.
  37. Going from <a> to <img>, or from <Article> to <Comment>,

    or from <Button> to <div> - any of those will lead to a full rebuild.
  38. Component instances receive componentWillUnmount(). When building up a new tree,

    new DOM nodes are inserted into the DOM. Component instances receive componentWillMount() and then componentDidMount(). Any state associated with the old tree is lost.
  39. Component instances receive componentWillUnmount(). When building up a new tree,

    new DOM nodes are inserted into the DOM. Component instances receive componentWillMount() and then componentDidMount(). Any state associated with the old tree is lost.
  40. React looks at the attributes of both, keeps the same

    underlying DOM node, and only updates the changed attributes
  41. By default, when recursing on the children of a DOM

    node, React just iterates over both lists of children at the same time and generates a mutation whenever there’s a difference.
  42. When children have keys, React uses the key to match

    children in the original tree with children in the subsequent tree.
  43. Now React knows that the element with key '9' is

    the new one, and the elements with the keys '10' and '20' have just moved.
  44. Keys should be stable, predictable, and unique. Unstable keys (like

    those produced by Math.random()) will cause many component instances and DOM nodes to be unnecessarily recreated.
  45. React provides a declarative API so that you don’t have

    to worry about exactly what changes on every update.