【发布时间】:2014-11-19 08:35:35
【问题描述】:
这是我的 JSX:
<h4>{post.title} <small> (by {post.author})</small> </h4>
这是在服务器上生成的(使用React.renderComponentToString())并发送到客户端。
<h4 data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0">
<span data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.0">Why JavaScript is eating the world.</span>
<span data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.1"> </span>
<small data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.2">
<span data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.2.0"> (by </span>
<span data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.2.1">spike</span>
<span data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.2.2">)</span>
</small>
<span data-reactid=".2ggx1vlvrwg.1.0.0.$posts-about.$2.0.3"></span>
</h4>
这是在客户端(在浏览器中)生成的:
<h4 data-reactid=".0.1.0.0.$posts-about.$2.0">
<span data-reactid=".0.1.0.0.$posts-about.$2.0.0">Why JavaScript is eating the world.</span>
<small data-reactid=".0.1.0.0.$posts-about.$2.0.1">
<span data-reactid=".0.1.0.0.$posts-about.$2.0.1.0"> (by </span>
<span data-reactid=".0.1.0.0.$posts-about.$2.0.1.1">spike</span>
<span data-reactid=".0.1.0.0.$posts-about.$2.0.1.2">)</span>
</small>
</h4>
显然缺少两个跨度,这导致了React attempted to use reuse markup in a container but the checksum was invalid。错误。
我想问题出在 JSX 编译器上。生成的捆绑并发送到客户端的 JavaScript 与服务器使用的不同。
有趣的是,如果我“修复” JSX 中的缩进,它就可以工作。服务器和客户端都生成相同的 HTML 标记,而没有这些额外的跨度。
<h4>{post.title}
<small> (by {post.author})</small>
</h4>
但是我没有阅读文档中关于小心宽度 JSX 缩进的任何内容,这让我有点紧张,因为这种问题很难(或至少令人讨厌)调试。
【问题讨论】: