16 Matching Annotations
  1. Sep 2022
    1. It works because in JavaScript, true && expression always evaluates to expression, and false && expression always evaluates to false. Therefore, if the condition is true, the element right after && will appear in the output. If it is false, React will ignore and skip it.

      Good example for the {var > 0 %% } type of expression.

    2. In JavaScript, class methods are not bound by default. If you forget to bind this.handleClick and pass it to onClick, this will be undefined when the function is actually called.

      Remember to BIND for Class Components or just USE arrow functions instead.

    3. Do Not Modify State Directly For example, this will not re-render a component: // Wrong this.state.comment = 'Hello'; Instead, use setState(): // Correct this.setState({comment: 'Hello'}); The only place where you can assign this.state is the constructor.

      Change state using setState() or with the hooks, changing the state out of this will NOT re-render the component.

    4. React is pretty flexible but it has a single strict rule: All React components must act like pure functions with respect to their props.

      ALWAYS REMEMBER, keep component props PURE, do NOT change the values of props. Use them and return something new instead.

    5. React treats components starting with lowercase letters as DOM tags. For example, <div /> represents an HTML div tag, but <Welcome /> represents a component and requires Welcome to be in scope.

      Always start component names with a capital letter.

    6. By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent XSS (cross-site-scripting) attacks.

      JSX Prevents Injection Attacks.

    7. Warning: Since JSX is closer to JavaScript than to HTML, React DOM uses camelCase property naming convention instead of HTML attribute names. For example, class becomes className in JSX, and tabindex becomes tabIndex.

      Threat it more like JS than HTML when it comes to conventions. Keep it camelCase.