《React Docs》

  • React is a JavaScript library

  • sparingly/recap/explicitly/synthetic/extract/subsequent/alternative/inclination/hatch

  • After compilation, JSX expressions become regular JavaScript objects.

  • React reads these objects and uses them to construct the DOM and keep it up to date.

  • Elements are the smallest building blocks of React apps.

  • React DOM takes care of updating the DOM to match the React elements.

  • Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

  • React elements are immutable. Once you create an element, you can't change its children or attributes. An element is like a single frame in a movie: it represents the UI at a certain point in time.

  • In practice, most React apps only call ReactDOM.render() once.

  • React Only Updates What's Necessary

  • When React sees an element representing a user-defined component, it passes JSX attributes to this component as a single object.

  • Always start component names with a capital letter.

  • Components can refer to other components in their output. This lets us use the same component abstraction for any level of detail.

  • Components must return a single root element.

  • Don't be afraid to split components into smaller components.

  • We recommend naming props from the component's own point of view rather than the context in which it is being used.

  • A good rule of thumb is that if a part of your UI is used several times (Button, Panel, Avatar), or is complex enough on its own (App, FeedStory, Comment), it is a good candidate to be a reusable component.

  • Whether you declare a component as a function or a class, it must never modify its own props.

  • All React components must act like pure functions with respect to their props.

  • State is similar to props, but it is private and fully controlled by the component. (Local state is exactly that: a feature available only to classes)

  • Clock(a component) is now defined as a class rather than a function. This lets us use additional features such as local state and lifecycle hooks.

  • In applications with many components, it's very important to free up resources taken by the components when they are destroyed.

  • The componentDidMount() hook runs after the component output has been rendered to the DOM.

  • If you don't use something in render(), it shouldn't be in the state.

  • There are three things you should know about setState().

    1. Do Not Modify State Directly (The only place where you can assign this.state is the constructor)
    2. State Updates May Be Asynchronous
    3. State Updates are Merged (The merging is shallow)
  • Any state is always owned by some specific component, and any data or UI derived from that state can only affect components "below" them in the tree.

  • If you imagine a component tree as a waterfall of props, each component's state is like an additional water source that joins it at an arbitrary point but also flows down.

  • Handling events with React elements is very similar to handling events on DOM elements. There are some syntactic differences:

    1. React events are named using camelCase, rather than lowercase.
    2. With JSX you pass a function as the event handler, rather than a string.
  • Another difference is that you cannot return false to prevent default behavior in React. You must call preventDefault explicitly.

  • When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered.

  • You have to be careful about the meaning of this in JSX callbacks. In JavaScript, class methods are not bound by default.

  • Conditional rendering in React works the same way conditions work in JavaScript.

  • You can use variables to store elements. This can help you conditionally render a part of the component while the rest of the output doesn't change.

  • Returning null from a component's render method does not affect the firing of the component's lifecycle methods. For instance, componentWillUpdate and componentDidUpdate will still be called.

  • A "key" is a special string attribute you need to include when creating lists of elements.

  • The best way to pick a key is to use a string that uniquely identifies a list item among its siblings.

  • We don't recommend using indexes for keys if the items can reorder, as that would be slow.

  • A good rule of thumb is that elements inside the map() call need keys.

  • Keys Must Only Be Unique Among Siblings. (Keys used within arrays should be unique among their siblings. However they don't need to be globally unique.)

  • Keys serve as a hint to React but they don't get passed to your components.

  • In React, mutable state is typically kept in the state property of components, and only updated with setState()
    .

  • In React, a