Skip to content

Latest commit

 

History

History
115 lines (87 loc) · 7.82 KB

React.md

File metadata and controls

115 lines (87 loc) · 7.82 KB

Reactivity

A Hands-on Introduction to Fine-Grained Reactivity Building a Reactive Library from Scratch

安装

yarn add react react-dom prop-types
yarn global add react-devtools
react-devtools

官方文档

学习资料

范例代码

JSX的优点

React’s JSX is fundamentally superior for a few simple reasons:

  • Leverage the Full Power of JavaScript
  • Compile-time Errors
  • Intellisense Support

基本概念

An element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.

A component can be declared in several different ways. It can be a class with a render() method. Alternatively, in simple cases, it can be defined as a function. In either case, it takes props as an input, and returns an element tree as the output.

When a component receives some props as an input, it is because a particular parent component returned an element with its type and these props. This is why people say that the props flows one way in React: from parents to children.

An instance is what you refer to as this in the component class you write. It is useful for storing local state and reacting to the lifecycle events. Functional components don’t have instances at all. Class components have instances, but you never need to create a component instance directly—React takes care of this.

React introduced a way to write the UI as a function of its state.

view = render(state)

React keeps the application UI in sync with the React state. Every re-render in React starts with a state change. When a component re-renders, it also re-renders all of its descendants. React doesn't re-render the entire app, only re-render the components which need update.

Each render is a snapshot, React plays a “find the differences” game to figure out what's changed between these two snapshots.

A pure component is one that always produces the same UI when given the same props. In the real world, many of our components are impure. When a component re-renders, it tries to re-render all descendants, regardless of whether they're being passed a particular state variable through props or not. So props have nothing to do with re-renders.

React.memo and React.PureComponent are two tools to allow us to ignore certain re-render requests. The idea of “memorization” is that React will remember the previous snapshot. If none of the props have changed, React will re-use that stale snapshot.

Why isn't “memorization” the default behaviour?

We tend to overestimate how expensive re-renders are. For simple components, re-renders are lightning quick. If a component has a bunch of props and not a lot of descendants, it can actually be slower to check if any of the props have changed compared to re-rendering the component.

The React team is actively investigating whether it's possible to “auto-memoize” code during the compile step.

context is sorta like “invisible props”, or maybe “internal props”.

组件分类

  • 纯展示型的组件,数据进,DOM 出,直观明了
  • 交互型组件,典型的例子是对于表单组件的封装和加强,大部分的组件库都是以交互型组件为主,比如说 Element UI,特点是有比较复杂的交互逻辑,但是是比较通用的逻辑,强调组件的复用
  • 接入型组件,在 React 场景下的 container component,这种组件会跟数据层的 service 打交道,会包含一些跟服务器或者说数据源打交道的逻辑,container 会把数据向下传递给展示型组件
  • 功能型组件,路由的 router-view 组件、transition 组件,本身并不渲染任何内容,是一个逻辑型的东西,作为一种扩展或者是抽象机制存在

服务端渲染SSR(Server-side rendering)

单页应用的前端框架,对搜索引擎不友好,SEO 效果差。 SSR的原理是服务器执行前端脚本,将渲染结果直接发给浏览器,从而提升 SEO 效果、加快网页显示。

Can not use window in componentWillMount and render methods.