深入理解react笔记

1. render函数没有渲染的作用,只是将jsx转译成一个react.createElement函数。

微信图片_20180503092057.jpg
JSX:
<button type="submit">
    Save
</button>
Transpiled JS:
React.createElement(
    "button",
    { type: "submit" },
    "Save"
);

2.state less:提倡无转态组件

蓝色为stateless,黑色为stateful


微信图片_20180503094134.jpg

这个组件什么都不画,只是做一个心跳动作

import React from 'react';

export default class HeartBeat extends React.Component {
  render() {
    return null;
  }

  componentDidMount() {
    this.timer = setInterval(() => {
      fetch('/api/v1/heartbeat');
    }, 5000);
  }

  componentWillUnmount() {
    clearInterval(this.timer);
  }
}

3.生命周期

  • mount
  1. componentDidMount 只有在客户端执行
  • update
    shouldcomponentupdate 可以截胡,提高性能
因state改变引发的update过程:

shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
因父组件想要render这个组件改变引发的update过程:


componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate

这些函数应该都是纯函数

  • unmount

3 高阶组件

函数,接收参数,返回一个全新组件

const HoC = (WrappedComponent) => {
    const WrappingComponent = (props) => (
        <div className="foo">
            <WrappedCompoent {...props} />
        </div>
    );
    return WrappingComponent;
};

可以多个参数动态创造不同的组件

const HoC = (WrappedComponent, LoginView) => {
    const WrappingComponent = () => {
         const {user} = this.props;  
         if (user) {
            return <WrappedComponent {...this.props} />
         } else {
            return <LoginView {...this.props} />
         }
    };
    return WrappingComponent;
};

4.通信

不推荐用ref来进行通信

5.key

1.兄弟唯一性
2.稳定性

三元表达式不需要用到key

6.总结

一切都是组件,可以是有渲染的,一个没有渲染的。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 自己最近的项目是基于react的,于是读了一遍react的文档,做了一些记录(除了REFERENCE部分还没开始读...
    潘逸飞阅读 3,468评论 1 10
  • HTML模版 之后出现的React代码嵌套入模版中。 1. Hello world 这段代码将一个一级标题插入到指...
    ryanho84阅读 6,281评论 0 9
  • 3. JSX JSX是对JavaScript语言的一个扩展语法, 用于生产React“元素”,建议在描述UI的时候...
    pixels阅读 2,873评论 0 24
  • 说在前面 关于 react 的总结过去半年就一直碎碎念着要搞起来,各(wo)种(tai)原(lan)因(le)。心...
    陈嘻嘻啊阅读 6,895评论 7 41
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 8,102评论 2 35