我有服務器這樣的代碼:如何處理具有同構React的子組件?
var data = {
scripts: scripts,
children:[<Comp1 />, <Comp2 />, <Comp3 />]
};
// keeping smaller for easier example than reality
var markup = '';
markup += '<script type="text/javascript">' +
'var PROPS = {' +
'Layout: ' + JSON.stringify(data) +
'};' +
'</script>';
markup += React.renderToString(
<Layout {...data} ></Layout>
);
所以服務器呈現樣樣精。然後,無論我嘗試什麼,我都會收到關於我如何處理孩子序列化以及在瀏覽器中重新使用時的各種警告:React.render(App(window.PROPS.Layout), document.getElementById('content'));
我的很多嘗試讓React抱怨我應該使用createFragment。但是當我這樣做的時候,我仍然會得到一些我應該包裝它的錯誤。
我的目標是渲染帶有幾個孩子的Layout組件,並讓瀏覽器中的React知道下面的相同元素。許多嘗試也產生此錯誤:
Warning: React attempted to reuse markup in a container but the checksum was invalid. This generally means that you are using server rendering and the markup generated on the server was not what the client was expecting. React injected new markup to compensate which works but you have lost many of the benefits of server rendering. Instead, figure out why the markup being generated is different on the client or server:
(client) d=".1frk89jhyio.1"></div><div data-react
(server) d=".1frk89jhyio.1"><div style="float:lef
我的客戶端代碼是這樣的:
var React = require('react'),
Layout = require('./components/layout');
React.render(<Layout {...PROPS.Layout} />, document.getElementById('content'));
你的數據繼承了大量的子對象/字符串+標記標記,你應該將它們串聯在一起 – syarul
@syarul當我把它們串在一起時,孩子們編碼,所以我看到頁面上的div而不是標記 –