zhangyanan0525 / learn_summaries

就是个学习集锦
5 stars 0 forks source link

重读react官方文档 #50

Open zhangyanan0525 opened 5 years ago

zhangyanan0525 commented 5 years ago

Tutorial

What is React?

React is a declarative, efficient, and flexible JavaScript library for building user interfaces.(React 是一个声明式,高效且灵活的用于构建用户界面的 JavaScript 库。)

Some concepts:

在 React 中,有一个命名规范,通常会将代表事件的监听 prop 命名为 on[Event],将处理事件的监听方法命名为 handle[Event] 这样的格式。

Modify-Data Conventions (Immutability )

image

image

Lifting State Up

image 当你遇到需要同时获取多个子组件数据,或者两个组件之间需要相互通讯的情况时,需要把子组件的 state 数据提升至其共同的父组件当中保存。之后父组件可以通过 props 将状态数据传递到子组件当中。这样应用当中所有组件的状态数据就能够更方便地同步共享了。

Function Components

image

zhangyanan0525 commented 5 years ago

Docs

MAIN CONCEPTS

1.Hello World

image

2.Introducing JSX

image JSX produces React “elements”. *** Why JSX components contain both markup and logic

3.Rendering Elements

image React elements are immutable.the only way to update the UI is to create a new element. React Only Updates What’s Necessary(Even though we create an element describing the whole UI tree, only the node whose contents has changed gets updated by React DOM.)

4.Components and Props

Conceptually, components are like JavaScript functions.They accept arbitrary inputs (called “props”) and return React elements describing what should appear on the screen.(组件,从概念上类似于 JavaScript 函数。它接受任意的入参(即 “props”),并返回用于描述页面展示内容的 React 元素。) we can use Function Components or Class Components Always start component names with a capital letter.(组件名字大写,小写会被认为是原生的) Props are Read-Only

5.State and Lifecycle

free up resources taken by the components when they are destroyed.(当组件被销毁时释放所占用的资源是非常重要的。) 比如:

  componentDidMount() {
    this.timerID = setInterval(***,1000);
  }
卸载时销毁
  componentWillUnmount() {
    clearInterval(this.timerID);
  }

关于state三件重要的事 1.Do Not Modify State Directly .Use setState(). 2.State Updates May Be Asynchronous 3.State Updates are Merged The Data Flows Down“top-down” or “unidirectional” data flow 数据是向下的。被称作,“自上而下”或者“单向的”数据流

6.Handling Events

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

class里的方法并不会默认绑定this。使用时注意绑定。 3种方式: 1.public class fields syntax 2.arrow function in the callback 3.binding in the constructor

Passing Arguments to Event Handlers(向事件处理程序传递参数): 1.显式传递 2.bind隐式传递

7.Conditional Rendering

1.if 2.&& operator, 3.JavaScript conditional operator condition ? true : false

8.Lists and Keys

zhangyanan0525 commented 5 years ago

Docs

ADVANCED GUIDES

Accessibility(无障碍)

Code-Splitting(代码分割)

1.import() The best way to introduce code-splitting into your app is through the dynamic import() syntax.(在你的应用中引入代码分割的最佳方式是通过动态 import() 语法。)When Webpack comes across this syntax, it automatically starts code-splitting your app. (当 Webpack 解析到该语法时,它会自动地开始进行代码分割) 2.React.lazy The React.lazy function lets you render a dynamic import as a regular component.React.lazy takes a function that must call a dynamic import(). This must return a Promise which resolves to a module with a default export containing a React component.(React.lazy 函数能让你像渲染常规组件一样处理动态引入(的组件)。React.lazy 接受一个函数,这个函数需要动态调用 import()。它必须返回一个 Promise,该 Promise 需要 resolve 一个 defalut export 的 React 组件。 )(这句是从别的文档上看来的:可以让我们在不依赖第三方库的情况下简化对延迟加载(lazy loading)的处理。) 3.Suspense 4.Error boundaries 5.Route-based code splitting 关于code-splitting 插播一点vue的知识 react和vue都实现的差不多啊哈哈哈哈哈哈 IMG_0046

Context

  1. Elements Of Different Types
    • tear down the old tree and build the new tree
    • componentWillUnmount() componentWillMount() componentDidMount()
  2. DOM Elements Of The Same Type only updates the changed attributes
  3. Component Elements Of The Same Type
    • state is maintained across renders. React updates the props。
    • componentWillReceiveProps() componentWillUpdate()
  4. Recursing On Children 列表children应该使用key

    Refs and the DOM

    1.When to Use Refs

    • Managing focus, text selection, or media playback.
    • Triggering imperative animations.
    • Integrating with third-party DOM libraries.

      2.Creating Refs

    • React.createRef()

      3.Accessing Refs

    • this.myRef.current

      4.You may not use the ref attribute on function components because they don’t have instances

      5.Exposing DOM Refs to Parent Components

    • ref forwarding

      6.回调函数方式的ref

      7.字符串方式的ref(可能会被废弃)

      Render Props

      Render Props, props.children function, HOC, React’s cloneElement()

      Static Type Checking

      Flow and TypeScript

      Strict Mode

    • 识别不安全的生命周期
    • 关于使用过时字符串 ref API 的警告
    • 关于使用废弃的 findDOMNode 方法的警告
    • 检测意外的副作用
    • 检测过时的 context API

      Typechecking With PropTypes

    • PropTypes
    • Requiring Single Child
    • Default Prop Values

      Uncontrolled Components

      Web Components

zhangyanan0525 commented 5 years ago

Api Reference

React

React Top-Level API

Overview

Components
Fragments