Open zhangyanan0525 opened 5 years ago
JSX produces React “elements”. *** Why JSX components contain both markup and logic
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.)
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
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 数据是向下的。被称作,“自上而下”或者“单向的”数据流
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隐式传递
1.if 2.&& operator, 3.JavaScript conditional operator condition ? true : false
受控组件:受控组件:<input/>, <textarea/>, <select/>
等
不受控组件: file input
等
react开发者工具检查状态提升的bug很不错
官方推荐用组合而不是继承。 在“Containment”关系的组建中,有些组建并不知道它的children是什么,比如dialog,siderbar等。可以使用props.children来传递children。有时候不使用props.children,我们也可以使用约定好的prop名字把组建传递进去。This approach may remind you of “slots” in other libraries but there are no limitations on what you can pass as props in React.(这种方法可能使你想起别的库中“槽”(slot)的概念,但在 React 中没有“槽”这一概念的限制,你可以将任何东西作为 props 进行传递。)
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都实现的差不多啊哈哈哈哈哈哈
Error boundaries are React components that catch JavaScript errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed.(错误边界是一种 React 组件,这种组件可以捕获并打印发生在其子组件树任何位置的 JavaScript 错误,并且,它会渲染出备用 UI,而不是渲染那些崩溃了的子组件树。) API:
Ref forwarding is a technique for automatically passing a ref through a component to one of its children. 一般我们用不着这个Ref forwarding。官方文档举了HOC使用Ref forwarding的例子。这里再附一篇文章
A common pattern in React is for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM. 有些情况确实不能拿个div包着一系列的元素。比如父组件是
跟第三库协同
1.Integrating with DOM Manipulation Plugins
集成带有 DOM 操作的插件
React is unaware of changes made to the DOM outside of React. if the same DOM nodes are manipulated by another library, React gets confused and has no way to recover.The easiest way to avoid conflicts is to prevent the React component from updating. You can do this by rendering elements that React has no reason to update, like an empty <div />
.
如果节点被react之外的其他的库改变了,react根本无感知。避免这种冲突最容易的方式就是阻止react 组件更新。比如我写一个空的<div/>
标签就可以不更新。
We will attach a ref to the root DOM element. Inside componentDidMount, we will get a reference to it so we can pass it to the jQuery plugin. To prevent React from touching the DOM after mounting, we will return an empty <div />
from the render() method. The<div />
element has no properties or children, so React has no reason to update it, leaving the jQuery plugin free to manage that part of the DOM:
我们在componentDidMount使用ref获取到这个element然后我们把我们的jquery插件放进去。为了阻止react还在操作这个dom我们就只写个空的div。这个div又没有children有没有属性,react当然不会更新它了。我们的jquery就控制这个dom。
detach event listeners in componentWillUnmount
别忘了在componentWillUnmount移除那些事件监听
we will also add a componentDidUpdate() lifecycle method that notifies if parent component’s state changes. we can use jQuery trigger() API to notify it about changes to the original DOM element.
我们在componentDidUpdate生命周期里使用jquery的api:trigger() 控制组件的更新。
2.Integrating with Other View Libraries
reactDOM.render()可以灵活处理
讲了jsx的多种用法。
性能优化
ReactDOM.createPortal(child, container) createPortal提供一种,可以把children render到父组件之外的其他DOM里的方法。 Even though a portal can be anywhere in the DOM tree, it behaves like a normal React child in every other way. Features like context work exactly the same regardless of whether the child is a portal, as the portal still exists in the React tree regardless of position in the DOM tree. 尽管 portal 可以被放置在 DOM 树中的任何地方,但在任何其他方面,其行为和普通的 React 子节点行为一致。由于 portal 仍存在于 React 树, 且与 DOM 树 中的位置无关,那么无论其子节点是否是 portal,像 context 这样的功能特性都是不变的。 This includes event bubbling. An event fired from inside a portal will propagate to ancestors in the containing React tree, even if those elements are not ancestors in the DOM tree. 这包含事件冒泡。一个从 portal 内部触发的事件会一直冒泡至包含 React 树的祖先,即便这些元素并不是 DOM 树 中的祖先。 我自己写了半拉Model。哈哈。样式很丑,但是能体现出potal的作用。1.Model组件能渲染在父组件之外的其他节点上2.Model里的按钮,点击冒泡依然能被最外层捕获。 codePen链接在这里
React算法基于以下两条假设: 1.Two elements of different types will produce different trees. 2.The developer can hint at which child elements may be stable across different renders with a key prop. (1.两个不同类型的元素会产生出不同的树;2.开发者可以通过 key prop 来暗示哪些子元素在不同的渲染下能保持稳定;) The Diffing Algorithm
Render Props, props.children function, HOC, React’s cloneElement()
Flow and TypeScript
Tutorial
What is React?
React is a declarative, efficient, and flexible JavaScript library for building user interfaces.(React 是一个声明式,高效且灵活的用于构建用户界面的 JavaScript 库。)
Some concepts:
Jsx Syntax
Naming Conventions
在 React 中,有一个命名规范,通常会将代表事件的监听 prop 命名为 on[Event],将处理事件的监听方法命名为 handle[Event] 这样的格式。
Modify-Data Conventions (Immutability )
Lifting State Up
当你遇到需要同时获取多个子组件数据,或者两个组件之间需要相互通讯的情况时,需要把子组件的 state 数据提升至其共同的父组件当中保存。之后父组件可以通过 props 将状态数据传递到子组件当中。这样应用当中所有组件的状态数据就能够更方便地同步共享了。
Function Components