2017-01-03 86 views
0

的多個錯誤,我得到了以下錯誤:與reactjs例如

bundle.js:1079 Warning: render(): Rendering components directly into document.body is discouraged, since its children are often manipulated by third-party scripts and browser extensions. This may lead to subtle reconciliation issues. Try rendering into a container element created for your app.printWarning @ bundle.js:1079warning @ bundle.js:1103_renderSubtreeIntoContainer @ bundle.js:11201render @ bundle.js:11271181../components/LoginForm/index.jsx @ bundle.js:20603s @ bundle.js:1e @ bundle.js:1(anonymous function) @ bundle.js:1 
bundle.js:1079 Warning: render(): Target node has markup rendered by React, but there are unrelated nodes as well. This is most commonly caused by white-space inserted around server-rendered markup.printWarning @ bundle.js:1079warning @ bundle.js:1103_renderSubtreeIntoContainer @ bundle.js:11241render @ bundle.js:11271181../components/LoginForm/index.jsx @ bundle.js:20603s @ bundle.js:1e @ bundle.js:1(anonymous function) @ bundle.js:1 
bundle.js:1079 Warning: validateDOMNesting(...): <html> cannot appear as a child of <body>. See body > App > html.printWarning @ bundle.js:1079warning @ bundle.js:1103validateDOMNesting @ bundle.js:17187mountComponent @ bundle.js:7165mountComponent @ bundle.js:12249performInitialMount @ bundle.js:6038mountComponent @ bundle.js:5925mountComponent @ bundle.js:12249performInitialMount @ bundle.js:6038mountComponent @ bundle.js:5925mountComponent @ bundle.js:12249mountComponentIntoNode @ bundle.js:10953perform @ bundle.js:14898batchedMountComponentIntoNode @ bundle.js:10975perform @ bundle.js:14898batchedUpdates @ bundle.js:9984batchedUpdates @ bundle.js:13020_renderNewRootComponent @ bundle.js:11169_renderSubtreeIntoContainer @ bundle.js:11250render @ bundle.js:11271181../components/LoginForm/index.jsx @ bundle.js:20603s @ bundle.js:1e @ bundle.js:1(anonymous function) @ bundle.js:1 
bundle.js:1079 Warning: Unknown DOM property charset. Did you mean charSet? 
    in meta (created by App) 
    in head (created by App) 
    in html (created by App) 
    in AppprintWarning @ bundle.js:1079warning @ bundle.js:1103validateProperty @ bundle.js:9503warnUnknownProperties @ bundle.js:9521handleElement @ bundle.js:9545onBeforeMountComponent @ bundle.js:9550callHook @ bundle.js:9587emitEvent @ bundle.js:9599onBeforeMountComponent @ bundle.js:9876mountComponent @ bundle.js:12246mountChildren @ bundle.js:11627_createContentMarkup @ bundle.js:7337mountComponent @ bundle.js:7204mountComponent @ bundle.js:12249mountChildren @ bundle.js:11627_createContentMarkup @ bundle.js:7337mountComponent @ bundle.js:7204mountComponent @ bundle.js:12249performInitialMount @ bundle.js:6038mountComponent @ bundle.js:5925mountComponent @ bundle.js:12249performInitialMount @ bundle.js:6038mountComponent @ bundle.js:5925mountComponent @ bundle.js:12249mountComponentIntoNode @ bundle.js:10953perform @ bundle.js:14898batchedMountComponentIntoNode @ bundle.js:10975perform @ bundle.js:14898batchedUpdates @ bundle.js:9984batchedUpdates @ bundle.js:13020_renderNewRootComponent @ bundle.js:11169_renderSubtreeIntoContainer @ bundle.js:11250render @ bundle.js:11271181../components/LoginForm/index.jsx @ bundle.js:20603s @ bundle.js:1e @ bundle.js:1(anonymous function) @ bundle.js:1 
bundle.js:1079 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) <meta data-reactid="3 
(server) <div data-reactid="6"printWarning @ bundle.js:1079warning @ bundle.js:1103_mountImageIntoNode @ bundle.js:11356mountComponentIntoNode @ bundle.js:10961perform @ bundle.js:14898batchedMountComponentIntoNode @ bundle.js:10975perform @ bundle.js:14898batchedUpdates @ bundle.js:9984batchedUpdates @ bundle.js:13020_renderNewRootComponent @ bundle.js:11169_renderSubtreeIntoContainer @ bundle.js:11250render @ bundle.js:11271181../components/LoginForm/index.jsx @ bundle.js:20603s @ bundle.js:1e @ bundle.js:1(anonymous function) @ bundle.js:1 
bundle.js:826 Uncaught Error: Unable to find element with ID 2.(…) 

我試圖跟隨反應教程https://facebook.github.io/react/tutorial/tutorial.html我必須說是相當差。另外,網絡上存在的教程是不同版本的混合體,有些使用不推薦使用的方法等等,它是一個叢林!

渲染方法:

'use strict'; 

import React from 'react'; 

export default class LoginForm extends React.Component { 
    constructor(props) { 
    super(props); 

    this.state = { isToggleOn: true }; 

    this.submitFormClick = this.submitFormClick.bind(this); 
    } 

    submitFormClick(e) { 
    e.preventDefault(); 

    this.setState(prevState => ({ 
     isToggleOn: !prevState.isToggleOn 
    })); 
    } 

    render() { 
    return (
     <div>{this.state.isToggleOn ? 'TRUE TOGGLE' : 'FALSE TOGGLE'} 
     <form role="form" onSubmit=""> 
      <input type="text" name="username" /> 
      <br /> 
      <input type="password" name="password" /> 
      <br /> 
      <button onClick={this.submitFormClick}>Login</button> 
     </form> 
     </div> 
    ); 
    } 
} 

入口方法index.js文件:

ReactDOM.render(<App />, document.getElementById('root')); 

main.html中:

<html> 

<head> 
    <meta charset="utf-8" /> 
    <title>index.html</title> 
</head> 

<body> 
    <div id="root"></div> 
    <script src="static/bundle.js"></script> 
</body> 

</html> 

我怎樣把它的服務器端:

const App = React.createFactory(AppComponent); 
const html = ReactDOMServer.renderToString(App({})); 

res.setHeader('Content-Type', 'text/html; charset=utf-8'); 
res.status(200) 
    .send(html); 

回答

0

您需要將此html傳遞到像這樣的html模板中。

const App = React.createFactory(AppComponent); 
const html = ReactDOMServer.renderToString(App({})); 
res.status(200).send(renderFullPage(html, null)); 

function renderFullPage(html, initialState) { 
    return ` 
    <!doctype html> 
    <html lang="en"> 
     <head> 

     <meta charset="utf-8"> 
     <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" /> 
     <title>React</title> 
     </head> 
     <body> 
     <div id="root">${html}</div> 
     <script> 
      window.__INITIAL_STATE__ = ${JSON.stringify(initialState)} 
     </script> 
     <script src="/static/bundle.js"></script> 
     </body> 
    </html> 
    ` 
}