Reactjs 为什么要在我的标题标签中添加注释块?
为什么react要在我的文本中添加注释块 我的代码:Reactjs 为什么要在我的标题标签中添加注释块?,reactjs,Reactjs,为什么react要在我的文本中添加注释块 我的代码: 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 {
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 data-reactroot="">
<head>
<title>Hello <!-- -->UNKNOWN</title>
</head>
<body>
</body>
</html>
从“React”导入React;
从“react dom/server”导入{renderToString};
从“快递”导入快递;
常量app=express();
app.listen(8080);
类MyPage扩展了React.Component{
render(){
返回(
你好{this.props.name | |“未知”}
);
}
}
app.get(“/”,(req,res)=>res.send(renderToString());
带有注释块的呈现HTML:
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 data-reactroot="">
<head>
<title>Hello <!-- -->UNKNOWN</title>
</head>
<body>
</body>
</html>
你好,未知
编辑#1:到目前为止,似乎唯一发生这种情况的地方是
标记
“看起来你的帖子大部分是代码;请添加更多细节。”:是的,我知道。但是没有其他需要添加的内容。您使用的是没有实际逻辑的| |(或)逻辑运算符,因此(我想,可能我错了)react会同时呈现this.props.name和未知字符串。我不知道为什么它会把它当作评论
我想尝试一下:
<head>
<title>Hello {this.props.name !== null ? this.props.name : "UNKNOWN"}</title>
</head>
你好{this.props.name!==null?this.props.name:“未知”}
编辑:在以下链接中发布了“问题”的解决方法及其原因:
这是经过设计的,有助于正确地反应这些文本节点
| |(or)运算符用法正确“this.props.name”未定义,因此该值将返回到“UNKNOWN”。问题是react也在添加“”。也许这会起作用,这里的一条评论发布了该问题的解决方法: