为什么要对我的标题标签添加评论栏?

时间:2020-10-21 12:06:58

标签: reactjs

为什么要对我的文本添加注释块?

我的代码:

import React from "react";
import { renderToString } from "react-dom/server";

import express from "express";
const app = express();
app.listen(8080);

class MyPage extends React.Component {
    render() {
        return (
            <html>
                <head>
                    <title>Hello {this.props.name || "UNKNOWN"}</title>
                </head>
            </html>
        );
    }
}

app.get("/", (req, res) => res.send(renderToString(<MyPage />)));

带有注释块的呈现HTML:

<html data-reactroot="">
<head>
<title>Hello <!-- -->UNKNOWN</title>
</head>
<body>
</body>
</html>

编辑#1:到目前为止,似乎发生这种情况的唯一地方是<title>标签。

“看来您的帖子大部分是代码;请添加更多详细信息。”:是的,我知道。但是没有什么可添加的。

1 个答案:

答案 0 :(得分:1)

您正在使用|| (或)没有实际逻辑的逻辑运算符,因此(我认为,也许我错了)react会同时呈现this.props.name和UNKNOWN字符串。我不知道为什么将它呈现为评论。

我会尝试:

<head>
      <title>Hello {this.props.name !== null ? this.props.name : "UNKNOWN"}</title>
</head>

编辑:在以下链接中发布了“问题”及其原因的解决方法:

https://github.com/facebook/react/issues/14725

这是设计使然,它可以帮助React正确地为这些文本节点补水。