惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
IT之家
IT之家
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
C
CERT Recently Published Vulnerability Notes
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
P
Palo Alto Networks Blog
Know Your Adversary
Know Your Adversary
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cisco Talos Blog
Cisco Talos Blog
L
Lohrmann on Cybersecurity
AWS News Blog
AWS News Blog
J
Java Code Geeks
博客园_首页
Scott Helme
Scott Helme
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
V
Visual Studio Blog
Cloudbric
Cloudbric
Jina AI
Jina AI
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 叶小钗
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
S
SegmentFault 最新的问题
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
W
WeLiveSecurity
K
Kaspersky official blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hacker News: Ask HN
Hacker News: Ask HN
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
量子位
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 三生石上(FineUI控件)
Recent Commits to openclaw:main
Recent Commits to openclaw:main

szhshp 的第三边境研究所

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 如果我用手搓了个暗物质雏形 | szhshp 的第三边境研究所 《Rust Programming Language》- EX - Cheatsheet | szhshp 的第三边境研究所 新的播放器上线了 | szhshp 的第三边境研究所 Cypress - End-to-End Testing: OKTA Authentication with MFA | szhshp 的第三边境研究所 VSC x VIM - 反正多学几个快捷键没有坏处 | szhshp 的第三边境研究所 两小时 Docker 速成 - Getting Started | szhshp 的第三边境研究所 《The Joy of Javascript》- 5 - Data | szhshp 的第三边境研究所 《The Joy of Javascript》- 4 - Meta Programming | szhshp 的第三边境研究所 《The Joy of Javascript》- 3 - ADT(Algebraic Data Type) | szhshp 的第三边境研究所 Web-Automation Testing | szhshp 的第三边境研究所 《The Joy of Javascript》- 2 - Functor/Monad | szhshp 的第三边境研究所 《The Joy of Javascript》- 1 - Object/Function | szhshp 的第三边境研究所 EvalError: Possible side-effect in debug-evaluate | szhshp 的第三边境研究所 前端 QA 工具链指南 - husky,commitlint,commitizen,eslint,typescript,express | szhshp 的第三边境研究所 效率工具 & 软件推荐 | szhshp 的第三边境研究所 站点开源 Project Titan | szhshp 的第三边境研究所 cmd/powershell-命令速查-Cheatsheet | szhshp 的第三边境研究所 Git-命令速查与相关问题解决-Cheatsheet & Troubleshooting | szhshp 的第三边境研究所 Tailwind.css not included in NextJS Production | szhshp 的第三边境研究所 Github GraphQL API - Data Integration | szhshp 的第三边境研究所 尝试 Windows Terminal + z.lua | szhshp 的第三边境研究所 StrokePlus.net - Powerful Hotkey Boost | szhshp 的第三边境研究所 优雅地 Hack 一台黑苹果 | szhshp 的第三边境研究所 一杯关东煮 | szhshp 的第三边境研究所 Gitee x PicGo 管理图床 | szhshp 的第三边境研究所 Create Your Own Redux (Typescript) | szhshp 的第三边境研究所 Getting Started: Ionic + Capacitor + React + Typescript + Android | szhshp 的第三边境研究所 GraphQL: File Upload & Troubleshooting | szhshp 的第三边境研究所 吉祥航空随心飞刷票工具循环查票工具 | szhshp 的第三边境研究所 NVM Windows Portable Installation | szhshp 的第三边境研究所 NextJS x Typescript - Integration & Troubleshooting | szhshp 的第三边境研究所 全站点升级 - Project Titan | szhshp 的第三边境研究所 AWS-EC2-常见问题解决-Troubleshooting | szhshp 的第三边境研究所 Workbox-Getting Started | szhshp 的第三边境研究所 《时间简史》阅读笔记 | szhshp 的第三边境研究所 《时间的秩序》阅读笔记 | szhshp 的第三边境研究所 ESLint-常见问题解决-Troubleshooting | szhshp 的第三边境研究所 站点索引 | szhshp 的第三边境研究所 个人知识库搭建 - Hexo 实现 Github/Coding 持续集成 | szhshp 的第三边境研究所 BMOB-常见问题解决-Troubleshooting | szhshp 的第三边境研究所 React + Typescript: 开启 HMR/Hot Loader | szhshp 的第三边境研究所 Yarn & NPM | szhshp 的第三边境研究所 ESLint: Typescript + React + TSX 集成 | szhshp 的第三边境研究所 《10101》EP15:过年 2020 | szhshp 的第三边境研究所 Cmder-Usage | szhshp 的第三边境研究所 Redux-DOM not refreshed | szhshp 的第三边境研究所 React Semantic UI-CheatSheet | szhshp 的第三边境研究所 Typescript: Getting Started | szhshp 的第三边境研究所 Flutter-Getting Started | szhshp 的第三边境研究所 Typescript: Access window object with type check | szhshp 的第三边境研究所 VSC Extension Development-Create A Code Formatter Extension | szhshp 的第三边境研究所 OneNote Extension-Main API | szhshp 的第三边境研究所 TortoiseGit Win10 Icon not showing correctly | szhshp 的第三边境研究所 Mongoose: aggregate() 方法实现聚合函数 | szhshp 的第三边境研究所 React In Depth - React 源码解析 - render() & Fiber | szhshp 的第三边境研究所 MongoDB x Mongoose: 实现类似 Join 的功能 | szhshp 的第三边境研究所 Disqus 评论框改造工程-实现 Disqus 代理的 Serverless 服务 | szhshp 的第三边境研究所 GUI, CLI, Terminal, Shell, TTY, Console, Bash | szhshp 的第三边境研究所 Jest Async Best Practise: No Nested Async | szhshp 的第三边境研究所 关系型数据库与非关系型数据库优缺点分析 | szhshp 的第三边境研究所 New Edge & Chromium Error - Package is invalid: 'CRX_VERSION_NUMBER_INVALID' | szhshp 的第三边境研究所 选择 GraphQL 的 N 个理由 | szhshp 的第三边境研究所 StrokePlus - 鼠标手势神器的代码编写技巧 | szhshp 的第三边境研究所 嗨,好久不见 | szhshp 的第三边境研究所 Vue + ElementUI 集成 Vue Router | szhshp 的第三边境研究所 IE8 - Unknown Runtime Error Using innerHTML & Ajax | szhshp 的第三边境研究所 React: Handling Events | szhshp 的第三边境研究所 React-Navigation Integration with Redux | szhshp 的第三边境研究所 React: States is tricky | szhshp 的第三边境研究所 《10101》EP14:小剧场 | szhshp 的第三边境研究所 ColdFusion - Basic - arguments in cffunction | szhshp 的第三边境研究所
重新解读React.Component | szhshp 的第三边境研究所
2018-02-28 · via szhshp 的第三边境研究所

Meta

目录

一篇笔记, 参考官方教程并且加上了很多批注

React: React.Component Overview

Overview

React.Component是一个抽象基类, 基本结构:

class Greeting extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

最小配置就是一个render()函数

The Component Lifecycle

Mounting

当一个component加载并且加入DOM的时候执行

These methods are called when an instance of a component is being created and inserted into the DOM:

Updating

当一个component重新渲染的时候执行

An update can be caused by changes to props or state. These methods are called when a component is being re-rendered:

Unmounting

删除时执行

This method is called when a component is being removed from the DOM:

Error Handling

当render的时候报错

This method is called when there is an error during rendering, in a lifecycle method, or in the constructor of any child component.

Functions

Render()

这个函数可以返回以下内容:

  • React elements. Typically created via JSX. An element can either be a representation of a native DOM component (<div />), or a user-defined composite component (<MyComponent />).
  • String and numbers. These are rendered as text nodes in the DOM.
  • Portals. Created with ReactDOM.createPortal.
  • null. Renders nothing.
  • Booleans. Render nothing. (Mostly exists to support return test && <Child /> pattern, where test is boolean.)

用于返回一个DOM时并且用上某些条件的情况

几个渲染限制
  • When returning null or false, ReactDOM.findDOMNode(this) will return null.
  • render() will not be invoked if shouldComponentUpdate() returns false.
使用的原则

The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it’s invoked, and it does not directly interact with the browser.

render()一般不要改变state里面的内容, 保证这个函数就做单一的渲染这个事情

If you need to interact with the browser, perform your work in componentDidMount() or the other lifecycle methods instead. Keeping render() pure makes components easier to think about.

Fragments

render()这个函数里面也可以返回一个DOM数组:

render() {
  return [
    <li key="A">First item</li>,
    <li key="B">Second item</li>,
    <li key="C">Third item</li>,
  ];
}

React V16.2之后也可以用这个方法

render() {
  return (
    <React.Fragment>
      <li>First item</li>
      <li>Second item</li>
      <li>Third item</li>
    </React.Fragment>
  );
}

区别就在于不需要提供key

constructor(props)

  • mount状态之前执行这个函数
  • 可以通过super(props)调用父类的方法
  • 并且如果不调用super(props)的话, this.props就会变成undefined
  • 应该在这个函数里初始化this.state
  • 并且不要在这个函数里使用setState()
  • 当然如果我需要绑定函数, 也没有状态变化的话, 就完全不需接入这个接口了

componentWillMount()

  • mount发生的时候立刻执行
  • render()之前执行
  • 但是和render()不一样, setState()之后, 不会重复执行一次

componentDidMount()

  • mount状态之后执行这个函数
  • 对于DOM的一系列init可以放在这个地方
  • 进行各类网络请求的最佳地点, 比如AJAX获取数据之类的
  • 注册subscription的最佳地点, 当然不要忘记在componentWillUnmount()注销subscription
  • 关于subscription
  • 就是类似于GQL之类的玩意
  • setState()之后, 重复执行一次
  • 上面这个用法的时候一定要注意性能问题

componentWillReceiveProps(nextProps)

  • 无论新的和旧的prop是否相等都会执行这个函数
  • 另外需要注意的是mount状态的时候并不会执行这个函数, 也就是说, 初始化的时候并不会执行这个函数

这个很容易理解了

有一些情况就是不通过state而通过props来刷新的情况, 就可以在这个函数里面判断新的和旧的prop是否相等, 并根据判断结果刷新state

shouldComponentUpdate(nextProps, nextState)

  • 在新的prop或者state接收到的时候, 并且在刷新之前执行
  • 默认情况下返回true, 这个函数会导致render()刷新
  • 另外就算是返回false, 也无法防止子模块的刷新
  • Currently, if shouldComponentUpdate() returns false, then componentWillUpdate(), render(), and componentDidUpdate()

componentWillUpdate(nextProps, nextState)

  • 在新的propsstate获取到之前执行
  • 因此可以在这儿执行一些update的准备活动
  • 当然不要在这里面修改stateprops
  • 如果非要修改的话, 最好使用componentWillReceiveProps()
  • 这个函数的是否执行与shouldComponentUpdate()的返回值相关

componentDidUpdate(prevProps, prevState)

  • 这个就是在更新时候执行
  • 对于一些DOM的处理可以放在这个地方
  • 另外可以在这个地方判断一下props是否改变, 并根据这个结果决定是否发送http请求
  • 这个函数的是否执行与shouldComponentUpdate()的返回值相关

componentWillUnmount()

  • 当元素销毁的时候执行, 因此适合执行一些善后工作
  • 比如计时器的关闭, 网络请求关闭, subscriptions的清理等等

componentDidCatch(error, info)

(待补充)

setState(updater[, callback])

  • 修改界面的主要方式
  • 最好将这个函数看成一个请求, 因为实现更新可能会有一定的延迟, 也有可能将一系列的更新批次完成
  • 正因为不会立刻执行, 对于state的读取就必须要小心, 推荐使用componentDidUpdate()
  • 除非shouldComponentUpdate()返回了false, 否则这个函数的执行一定会重新渲染界面
  • 因此通过一定条件判断是否使用这个函数, 可以避免一些不必要的重新渲染
使用方法
  • 第一个参数是个updater function
  • 第一个参数也可以直接放一个Obj, 会被自动merge到state
  • 第二个参数是一个callback, 当state修改之后执行
updater的使用方法
this.setState((prevState, props) => {
  return {counter: prevState.counter + props.step};
});
updater的使用方法2
this.setState({quantity: 2})
异步执行

这个函数的执行是异步的, 因此如果有这样的执行:

Object.assign(
  previousState,
  {quantity: state.quantity + 1},
  {quantity: state.quantity + 1},
  ...
)

最后可能仅仅加了1

因此如果要改变的状态和历史状态有关, 那么最好使用updater函数

this.setState((prevState) => {
  return {quantity: prevState.quantity + 1};
});

component.forceUpdate(callback)

  • 对于当前模块强行调用render(), 并且忽略shouldComponentUpdate()的限制
  • 同时也会调用所有子模块的render(), 但是会根据它们的生命周期判断shouldComponentUpdate()的值
  • 当然不推荐使用这个方法, 尽可能仅仅通过props以及state进行重新渲染

Class Properties

defaultProps

这个是直接给一个模块提供默认的prop

class CustomButton extends React.Component {
  // ...
}

CustomButton.defaultProps = {
  color: 'blue'
};

......

  render() {
    return <CustomButton /> ; // props.color will be set to blue
  }

但是注意, 不管给任何值, 只要给了值, 就一定会覆盖掉默认值

  render() {
    return <CustomButton color={null} /> ; // props.color will remain null
  }

displayName

这个其实是debug的时候方便查看用的

相关文章: https://reactjs.org/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging

Instance Properties

props

没什么好说的了

  • this.props.children这个比较特别,这个包含了所有子标签的JSX

state

  • 永远不要直接修改这个词一定要通过setState()来进行修改