React库中的context和updater参数是什么? [英] What are context and updater arguments in the React library?

查看:165
本文介绍了React库中的context和updater参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过了解React React库,但无法理解 context updater 是什么,它被传递到零件。

I was trying to understand the React through the React library, but couldn't understand what context and updater is, which is passed in to the Component.

以下是图书馆中的代码。

Following was the code in the library.

function Component(props, context, updater) {
  this.props = props;
  this.context = context;
  this.refs = emptyObject;
  // We initialize the default updater but the real one gets injected by the
  // renderer.
  this.updater = updater || ReactNoopUpdateQueue;
}

这些东西的目的是什么?

What is the purpose of these things?

推荐答案

背景



context 的目的是让开发者直接传递 props 组件,而不是通过 /的道具 父母(可能会非常复杂/混乱)。

Context

The purpose of context is to enable devs to pass props directly to components, rather than via the props of children / parents (which can get very complicated / messy).


在某些情况下,您希望通过组件树传递数据,而无需在每个级别手动传递道具。您可以使用功能强大的上下文API直接在React中执行此操作。

In some cases, you want to pass data through the component tree without having to pass the props down manually at every level. You can do this directly in React with the powerful "context" API.






更新程序



updater 是一个对象,其中包含方法更新 DOM


Updater

updater is an object containing methods to update the DOM.

这在行<$中很明显c $ c> 61 79

// Line 61: Enqueue setState.
this.updater.enqueueSetState(this, partialState, callback, 'setState') 

// Line 79: Force Update.
this.updater.enqueueForceUpdate(this, callback, 'forceUpdate') 

这些<$使用 触发c $ c> methods setState() forceUpdate()

These methods are triggered using setState() and forceUpdate() respectively.

这篇关于React库中的context和updater参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆