产生这是我的JSX:不同的标记在服务器和客户端从同一个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与服务器使用的JavaScript不同。
有趣的是,如果我在JSX中“修复”了缩进,它就可以工作。服务器和客户端都会生成相同的HTML标记,而没有这些额外的跨度。
<h4>{post.title}
<small> (by {post.author})</small>
</h4>
但是我还没有关于小心宽度JSX压痕的文档中读什么,这让我有点紧张,因为这样那样的问题是很难(或者至少烦人)调试。
感谢您的澄清,但我仍然不明白为什么它在客户端和服务器上的行为不同。编译器和规则应该是相同的。 – tobik 2014-09-24 18:58:33
@tobik不久前对空白规则进行了更改,因此您可能在其中一个环境中使用了较旧版本的编译器。 – FakeRainBrigand 2014-09-24 21:23:09
好吧,就是这样。我正在使用reactify的过时版本。升级到最新版本后,问题消失。编译现在在服务器和客户端都是一致的。我会将其作为单独的答案发布。感谢您的帮助。 – tobik 2014-09-24 21:44:05