简体   繁体   English

序列化时未显示React.Element对象内部

[英]React.Element object internals are not shown when serialized

I can have a functional component like this: 我可以有一个这样的功能组件:

const FuncList = ({ name }) => {
    return (
      <div className="shopping-list">
        <h1>Shopping List for {name}</h1>
        <ul>
          <li>Instagram</li>
          <li>WhatsApp</li>
          <li>Oculus</li>
        </ul>
      </div>
    )
}

I can instantiate it to see basically the entire element content: 我可以实例化它,以查看基本上整个元素的内容:

const obj = FuncList({ name: 'Pete' })
console.log(JSON.stringify(obj, null, 4))
-----
{
    'type': 'div',
    'key': null,
    'ref': null,
    'props': {
        'className': 'shopping-list',
        'children': [
            {
                'type': 'h1',
                'key': null,
                'ref': null,
                'props': {
                    'children': [
                        'Shopping List for ',
                        'mike'
                    ]
                },
                '_owner': null,
                '_store': {}
            },
            {
                'type': 'ul',
                'key': null,
                'ref': null,
                'props': {
                    'children': [
                        {
                            'type': 'li',
                            'key': null,
                            'ref': null,
                            'props': {
                                'children': 'Instagram'
                            },
                            '_owner': null,
                            '_store': {}
                        },
                        ....
                    ]
                },
                '_owner': null,
                '_store': {}
            }
        ]
    },
    '_owner': null,
    '_store': {}
}

Now when I use it with JSX syntax this: 现在,当我将其与JSX语法一起使用时,这是:

const obj = <JSXList name="Pete" />;
// same as: const obj = React.createElement(JSXList, { name: "Pete" });
console.log(JSON.stringify(obj, null, 4))

I get almost empty object. 我几乎得到空的物体。 Where is the link to JSXList stored? JSXList的链接存储在哪里? Where are all the properties? 所有物业在哪里? Are they hidden inside the object somehow? 它们是否以某种方式隐藏在对象内部? It looks like 'return' method of JSXList was never called, thus it was never expanded, but why there's no reference to it? 似乎从未调用过JSXList的“返回”方法,因此也从未对其进行扩展,但是为什么没有对此进行引用?

{
    'key': null,
    'ref': null,
    'props': {
        'name': 'Pete'
    },
    '_owner': null,
    '_store': {}
}

If I would use 'div' instead of JSXLint as a first argument, I would get at least 'type' property which would indicate the purpose of the element. 如果我将'div'代替JSXLint作为第一个参数,则至少会得到'type'属性,该属性指示元素的用途。 Is there any design rationale behind that? 其背后是否有任何设计依据?

const obj = React.createElement('div', { name: "Pete" });
console.log(JSON.stringify(obj, null, 4))

Is there any point in hiding that reference? 隐藏参考有什么意义吗? It sort of obscure object introspection, as far as I can tell. 据我所知,它有点模糊的对象自省。

You can use codepad to play around if you like. 如果愿意,您可以使用键盘进行游戏。

I get almost empty object. 我几乎得到空的物体。 Where is the link to JSXList stored? JSXList的链接存储在哪里?

In obj.type. 在obj.type中。 You're not seeing type in the log because functions are not serializable, so JSON.stringify skips them. 您不会在日志中看到类型,因为函数不可序列化,因此JSON.stringify会跳过它们。

Where are all the properties? 所有物业在哪里?

It's there. 在那。 You only gave it one property: name: 'pete' . 您只给了它一个属性: name: 'pete' If you're expecting to see divs and such, the reason you're not is that the following two lines are not equivalent: 如果您期望看到div等,则您之所以不这样做,是因为以下两行相等:

const obj = FuncList({ name: 'Pete' })
const obj = <FuncList name="Pete" />;

As you pointed out in a comment in your code, the jsx expands to this, which again is not equivalent to calling FuncList: 正如您在代码中的注释中指出的那样,jsx会扩展为此,这再次不等同于调用FuncList:

React.createElement(FuncList, { name: "Pete" });

React.createElement creates a small object describing what to render, which is the small object you're seeing. React.createElement创建一个小对象,描述要渲染的内容,即您正在看到的小对象。 What normally happens next is that react's reconciliation algorithm decides whether it needs to take things farther. 接下来通常发生的是,react的对帐算法决定是否需要进一步处理。 If it does, then only then will it instantiate the FuncList, calling FuncList({ name: 'Pete' }) and producing the larger object. 如果是这样,则只有这样,它将实例化FuncList,调用FuncList({ name: 'Pete' })并生成更大的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM