byte-fe / intern-study

实习生互助学习
MIT License
33 stars 6 forks source link

简单过一眼react文档,记录我留意的点。 #19

Open deligent-ant opened 6 years ago

deligent-ant commented 6 years ago

简单过一眼react文档,记录我留意的点。

这是从官方文档中记录,个人觉得比较需要留点心眼的知识,并不是为了介绍react。
所以不会介绍reacts生命周期、setState等知识。

  1. React 可以将多个setState() 调用合并成一个调用来提高性能。 React 中另一个不同是你不能使用返回 false 的方式阻止默认行为.

    <button onClick={this.handleClick}>

    你必须谨慎对待 JSX 回调函数中的 this,类的方法默认是不会绑定 this 的: // This binding is necessary to make this work in the callback

    • 方法一:
      this.handleClick = this.handleClick.bind(this);
    • 方法二:
      <button onClick={(e) => this.handleClick(e)}>
    • 方法三:
      <button onClick={this.deleteRow.bind(this, id)}>
  2. 用花括号包裹代码在 JSX 中嵌入任何表达式

    <div>
      <h1>Hello!</h1>
      {unreadMessages.length > 0 &&
        <h2>
          You have {unreadMessages.length} unread messages.
        </h2>
      }
    </div>
  3. 数组元素中使用的key在其兄弟之间应该是独一无二的。然而,它们不需要是全局唯一的。 key会作为给React的提示,但不会传递给你的组件。 如果您的组件中需要使用和key相同的值,请将其作为属性传递.

  4. 在HTML当中,