简体   繁体   中英

Multiple errors with reactjs example

I get the following errors:

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

I'm trying to follow the react tutorial https://facebook.github.io/react/tutorial/tutorial.html which I must say is pretty poor. Also the tutorials which exist on the net are a mix of the different versions which some using deprecated methods etc. it's a jungle!

render method:

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

entrance method in index.js file:

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>

How I render it server side:

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

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

You need to pass this html into a html template like this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM