我正在尝试以同构方式呈现react,它呈现但我在客户端收到警告/错误说:
我使用 jspm 和 npm 作为包管理器;
warning.js:25 Warning: render(...): Replacing React-rendered children with a new root component. If you intended to update the children of this node, you should instead have the existing children update their state and render the new components instead of calling ReactDOM.render.
服务器输出的源代码renderToString
:
<div id="reactRoot"><div data-reactroot="" data-reactid="1" data-react-checksum="845161664"><marquee data-reactid="2"><h1 data-reactid="3">App</h1></marquee></div></div>
渲染后由 react 替换的源:
<div id="reactRoot"><div data-reactid=".0"><marquee data-reactid=".0.0"><h1 data-reactid=".0.0.0">App</h1></marquee></div></div>
快递服务器中间件:
import App from './components/App/App.jsx';
// [...] Express code removed for brevity
app.use('*', (req, res, next) => {
try {
res.render('index.html', {
reactHtml: renderToString(
<App />
)
});
} catch (err) {
next(err);
}
});
的index.html
:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>App</title>
<script>
console.log('Running on <%- @env %> environment!');
<% if (@env == 'development') : %>
System.import('systemjs-hot-reloader/default-listener.js');
<% end %>
System.import('/app.jsx!')
.catch(console.error.bind(console));
</script>
</head>
<body>
<div id="reactRoot"><%- reactHtml %></div>
</body>
</html>
我在这里使用 ect 作为模板引擎;
的app.jsx
:
import { render } from 'react-dom';
import App from './components/App/App.jsx';
const mountPoint = document.getElementById('reactRoot');
render(
<App />,
mountPoint
);
的App/App.jsx
:
import React from 'react';
const App = () => (
<div>
<marquee><h1>App</h1></marquee>
</div>
);
export default App;