2017-01-03 7 views
0

ich folgende Fehler:Mehrere Fehler mit reactjs Beispiel

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.(…) 

Ich versuche, die reagieren Tutorial https://facebook.github.io/react/tutorial/tutorial.html zu folgen, muss ich sagen, ziemlich schlecht ist. Auch die Tutorials, die im Internet existieren, sind eine Mischung aus den verschiedenen Versionen, die einige veraltete Methoden verwenden, usw. Es ist ein Dschungel!

Methode machen:

'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> 
    ); 
    } 
} 

Eingang Methode in index.js Datei:

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> 

Wie ich machen es Server-Seite:

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

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

Antwort

0

Sie müssen diese HTML in eine HTML-Vorlage wie folgt übergeben.

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> 
    ` 
}