简体   繁体   中英

React Js: mountNode is not defined no-undef error

I am newly learning the React Js. I found the example at this Link . But when I tried the first code:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import ReactDOM from 'react-dom';

class HelloMessage extends React.Component {
  render() {
    return (
      <div>
        Hello {this.props.name}
      </div>
    );
  }
}

ReactDOM.render(
  <HelloMessage name="Taylor" />,
  mountNode
);
export default HelloMessage;

I am getting this error

./src/App.js Line 18: 'mountNode' is not defined no-undef

Search for the keywords to learn more about each error.

I have already seen the answer at this StackOverflow link . But I'm sorry I couldn't get what is explained there. Provide me the suggestions. Thank you in advance!

The error message you are getting is a linting error. (static code analysis)

Make sure your mountNode variable exists.

or use something like:

render(<HelloMessage />, document.getElementById('app'));

Also make sure that you have a DOM element with id app in your HTML code:

for example:

<div id="app" />

The ReactDOM.render() method is already located under

src/index.js

like:

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

The above code renders over component in the root div located in the public/index.html I understood that... We need to add the HelloMessage component in the

src/App.js

But --->initially it looked like:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}

export default App;

Now instead of rendering the App Component...we can either write the HelloMessage component under the same file or replace the App Component with HelloMessage Component. What I have did is..

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
      </div>
    );
  }
}
//Added the HelloMessage Component
class HelloMessage extends React.Component {
  render() {
    return (
      <div>
        Hello {this.props.name}
      </div>
    );
  }
}
//Exported the HelloMessage Component to index.js file instead of App Component
export default HelloMessage;

After that I'm able to see the Hello Message in the browser localhost:3000 . But the Name Taylor is not displayed there...So what I did is passed the name props from the index.js file something like:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
//Passed the name props to the
ReactDOM.render(<App name = "Taylor"/>, document.getElementById('root'));
registerServiceWorker();

Now After this point I got the successful output Hello Taylor . Also make sure if you want are replacing the App component with HelloMessage component. Don't forget to import that in the index.js file as

import HelloMessage from './App' 
...

Also replace the <App name = "Taylor"/> with <HelloMessage name = "Taylor"/>

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