资讯专栏INFORMATION COLUMN

React源码解析之React.createContext()

booster / 3182人阅读

摘要:我们只希望最多有两个并发渲染器主要和次要主要和次要。辅助渲染器将自己的的存储在多带带的字段中。

前言:
由于childContextReact17中会被废弃,所以不去分析它了,主要是新 API— —createContext()的讲解

一、React.createContext()

作用:
方便祖先组件与后代组件(中间隔了好多层组件)传值

使用:
context.js:

</>复制代码

  1. import React from "react";
  2. const contextTestOne={
  3. name:"chen",
  4. length:22,
  5. }
  6. export const wrapContext=React.createContext(contextTestOne.name)

祖先组件:

</>复制代码

  1. import { wrapContext } from "@/utils/context";
  2. const Father=props=>{
  3. return (
  4. )
  5. }

子孙组件:

</>复制代码

  1. import { wrapContext } from "@/utils/context";
  2. const getProviderValue=()=>{
  3. return {value=>{value}}
  4. }
  5. const Child=props=>{
  6. return (
  7. getProviderValue()
  8. );
  9. }

结果:

注意:
undefined传递给value时,createContext中的defaultValue不会生效,Consumervalue显示空值

React 官方文档:
https://zh-hans.reactjs.org/docs/context.html#contextprovider

源码:

</>复制代码

  1. /**
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @flow
  8. */
  9. import {REACT_PROVIDER_TYPE, REACT_CONTEXT_TYPE} from "shared/ReactSymbols";
  10. import type {ReactContext} from "shared/ReactTypes";
  11. import warningWithoutStack from "shared/warningWithoutStack";
  12. import warning from "shared/warning";
  13. export function createContext(
  14. defaultValue: T,
  15. //使用Object.is()计算新老context的差异
  16. calculateChangedBits: ?(a: T, b: T) => number,
  17. ): ReactContext {
  18. if (calculateChangedBits === undefined) {
  19. calculateChangedBits = null;
  20. } else {
  21. //不看
  22. if (__DEV__) {
  23. warningWithoutStack(
  24. calculateChangedBits === null ||
  25. typeof calculateChangedBits === "function",
  26. "createContext: Expected the optional second argument to be a " +
  27. "function. Instead received: %s",
  28. calculateChangedBits,
  29. );
  30. }
  31. }
  32. const context: ReactContext = {
  33. //还是那句话,ReactContext中的$$typeof
  34. // 作为createElement中的属性type中的对象进行存储的,并不是ReactElement的$$typeof
  35. $$typeof: REACT_CONTEXT_TYPE,
  36. _calculateChangedBits: calculateChangedBits,
  37. //作为支持多个并发渲染器的解决方法,我们将一些渲染器分类为主要渲染器,将其他渲染器分类为辅助渲染器。
  38. // As a workaround to support multiple concurrent renderers, we categorize
  39. // some renderers as primary and others as secondary.
  40. //我们只希望最多有两个并发渲染器:React Native(主要)和Fabric(次要);
  41. // React DOM(主要)和React ART(次要)。
  42. // 辅助渲染器将自己的context的value存储在多带带的字段中。
  43. // We only expect
  44. // there to be two concurrent renderers at most: React Native (primary) and
  45. // Fabric (secondary); React DOM (primary) and React ART (secondary).
  46. // Secondary renderers store their context values on separate fields.
  47. //中的value就是赋值给_currentValue的
  48. //也就是说_currentValue和_currentValue2作用是一样的,只是分别给主渲染器和辅助渲染器使用
  49. _currentValue: defaultValue,
  50. _currentValue2: defaultValue,
  51. // Used to track how many concurrent renderers this context currently
  52. // supports within in a single renderer. Such as parallel server rendering.
  53. //用来追踪该context的并发渲染器的数量
  54. _threadCount: 0,
  55. // These are circular
  56. Provider: (null: any),
  57. Consumer: (null: any),
  58. };
  59. //const obj={}
  60. //obj.provider._obj = obj
  61. context.Provider = {
  62. $$typeof: REACT_PROVIDER_TYPE,
  63. _context: context,
  64. };
  65. let hasWarnedAboutUsingNestedContextConsumers = false;
  66. let hasWarnedAboutUsingConsumerProvider = false;
  67. //不看
  68. if (__DEV__) {
  69. // A separate object, but proxies back to the original context object for
  70. // backwards compatibility. It has a different $$typeof, so we can properly
  71. // warn for the incorrect usage of Context as a Consumer.
  72. const Consumer = {
  73. $$typeof: REACT_CONTEXT_TYPE,
  74. _context: context,
  75. _calculateChangedBits: context._calculateChangedBits,
  76. };
  77. // $FlowFixMe: Flow complains about not setting a value, which is intentional here
  78. Object.defineProperties(Consumer, {
  79. Provider: {
  80. get() {
  81. if (!hasWarnedAboutUsingConsumerProvider) {
  82. hasWarnedAboutUsingConsumerProvider = true;
  83. warning(
  84. false,
  85. "Rendering is not supported and will be removed in " +
  86. "a future major release. Did you mean to render instead?",
  87. );
  88. }
  89. return context.Provider;
  90. },
  91. set(_Provider) {
  92. context.Provider = _Provider;
  93. },
  94. },
  95. _currentValue: {
  96. get() {
  97. return context._currentValue;
  98. },
  99. set(_currentValue) {
  100. context._currentValue = _currentValue;
  101. },
  102. },
  103. _currentValue2: {
  104. get() {
  105. return context._currentValue2;
  106. },
  107. set(_currentValue2) {
  108. context._currentValue2 = _currentValue2;
  109. },
  110. },
  111. _threadCount: {
  112. get() {
  113. return context._threadCount;
  114. },
  115. set(_threadCount) {
  116. context._threadCount = _threadCount;
  117. },
  118. },
  119. Consumer: {
  120. get() {
  121. if (!hasWarnedAboutUsingNestedContextConsumers) {
  122. hasWarnedAboutUsingNestedContextConsumers = true;
  123. warning(
  124. false,
  125. "Rendering is not supported and will be removed in " +
  126. "a future major release. Did you mean to render instead?",
  127. );
  128. }
  129. return context.Consumer;
  130. },
  131. },
  132. });
  133. // $FlowFixMe: Flow complains about missing properties because it doesn"t understand defineProperty
  134. context.Consumer = Consumer;
  135. }
  136. else {
  137. //const obj={}
  138. //obj.consumer=obj
  139. //也就是Consumber对象指向React.Context对象
  140. //在进行渲染时,为了保证Consumer拿到最新的值,
  141. //直接让Consumer=React.Context,
  142. // React.Context中的_currentValue已经被的value给赋值了
  143. //所以Consumer能立即拿到最新的值
  144. context.Consumer = context;
  145. }
  146. //不看
  147. if (__DEV__) {
  148. context._currentRenderer = null;
  149. context._currentRenderer2 = null;
  150. }
  151. return context;
  152. }

解析:
不看__DEV__的话,还是挺简单的,需要注意的是context.Consumer = context,让等于React.context,这样能立即拿到提供的最新值

二、为什么要弃用childContext
因为childContext对下层的组件影响太大了,即使子孙组件没有用到childContext,子孙组件仍然要进行Update,严重影响了性能

(完)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/106160.html

相关文章

  • React 新特性讲解及实例(一)

    摘要:接收一个属性,这个组件会让后代组件统一提供这个变量值。因此对于同一个对象而言,一定是后代元素。解决方法就是把内联函数提取出来,如下讲了这么多,我们还没有讲到其实我们已经讲完了的工作原理了。 本节主要讲解以下几个新的特性: Context ContextType lazy Suspense 错误边界(Error boundaries) memo 想阅读更多优质文章请猛戳GitHub博...

    Betta 评论0 收藏0
  • [ 一起学React系列 -- 4 ] 透传的Context

    摘要:官方对的介绍是意思就是提供了一种通过组件树传递数据的方法,而无需在每个级别手动传递。这也是基于重要物证哈哈实例使用学习技术最终是要有产出的。依然被视作一个组件,不过不同的是它的子组件必须是一个方法并且该方法接收当前对象并最终返回一个节点。 抛转引玉 通过上一篇的科普我们知道如果父节点需要向子节点传递数据,那么就得通过Props来实现;那么摆在我们眼前的就有一个问题了:现有N个节点并且它...

    firim 评论0 收藏0
  • [源码阅读]高性能和可扩展的React-Redux

    摘要:到主菜了,先看它的一看,我们应该有个猜测,这货是个高阶函数。可能有点绕,但就是这么一个个高阶函数组成的,后面会详细说。定义了一个处理函数和高阶函数执行次的方法,这个方法比上面的复杂在于它需要检测参数是否订阅了。 注意:文章很长,只想了解逻辑而不深入的,可以直接跳到总结部分。 初识 首先,从它暴露对外的API开始 ReactReduxContext /* 提供了 React.creat...

    shuibo 评论0 收藏0
  • 精读《React16 新特性》

    摘要:引言于发布版本,时至今日已更新到,且引入了大量的令人振奋的新特性,本文章将带领大家根据更新的时间脉络了解的新特性。其作用是根据传递的来更新。新增等指针事件。 1 引言 于 2017.09.26 Facebook 发布 React v16.0 版本,时至今日已更新到 React v16.6,且引入了大量的令人振奋的新特性,本文章将带领大家根据 React 更新的时间脉络了解 React1...

    Nosee 评论0 收藏0
  • React Hooks 解析(下):进阶

    摘要:第一次了解这项特性的时候,真的有一种豁然开朗,发现新大陆的感觉。在绝大多数情况下,是更好的选择。唯一例外的就是需要根据新的来进行操作的场景。会保证在页面渲染前执行,也就是说页面渲染出来的是最终的效果。上面条规则都是为了保证调用顺序的稳定性。 欢迎关注我的公众号睿Talk,获取我最新的文章:showImg(https://segmentfault.com/img/bVbmYjo); 一、...

    APICloud 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<