react.js - React 组件对象的属性 $$typeof 能有什么应用?

查看:185
本文介绍了react.js - React 组件对象的属性 $$typeof 能有什么应用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

如题 React 组件对象的属性 $$typeof 能有什么应用?

解决方案

一个标志,表示其为一个React Element, 源码如下,有英文解释

// The Symbol used to tag the ReactElement type. If there is no native Symbol
// nor polyfill, then a plain number is used for performance.
var REACT_ELEMENT_TYPE =
  (typeof Symbol === 'function' && Symbol.for && Symbol.for('react.element')) ||
  0xeac7;


var ReactElement = function(type, key, ref, self, source, owner, props) {
  var element = {
    // This tag allow us to uniquely identify this as a React Element
    $$typeof: REACT_ELEMENT_TYPE,

    // Built-in properties that belong on the element
    type: type,
    key: key,
    ref: ref,
    props: props,

    // Record the component responsible for creating this element.
    _owner: owner,
  };
  .....

}

补充一下,它存在的作用。

没有加这个$$typeof之前,object被认为是有效的React Element。为了将React Element和普通的对象区分开,

判断React的children是否是一个React Element, 来尽可能防止xss漏洞。

相关文章如下:

https://github.com/facebook/r...

http://danlec.com/blog/xss-vi...

这篇关于react.js - React 组件对象的属性 $$typeof 能有什么应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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