0

I'm trying to create a React component for a navigation bar.

This component I'd like to import from a separate file into my App.js. Currently, the component should just return a simple 'Hello world' paragraph, but I have trouble getting this to work.

I have written the following code into a file located at src/components/navbar.js:

import React from 'react';

export default class navBar extends React.Component {
    render() {
        return (
            <p>Hello world.</p>
        )
    }
}

Now I'd like to import this component from my src/App.js, which looks like this:

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

import navBar from './components/navbar.js'

class App extends Component {
    render() {
        return (
            <navBar/>
        );
    }
}

export default App;

If I compile and open the site, nothing's there, which confuses me.

I'd be very thankful for any help!

EDIT: It's been suggested that the problem is that <App /> is not being rendered anywhere. I don't believe that's the case, since there's another file being created by default (index.js), which looks like this:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

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

serviceWorker.unregister();

I have also tried putting the paragraph (and the entire navbar) directly into src/App.js.

After compiling I could see the expected results in the browser, so the problem should lie with the exporting/importing.

8
  • 1
    How do you compile the site? How do you run the server? Is there anything in the console? Commented Dec 30, 2018 at 23:13
  • 2
    Is this the entire code you have? Because if so <App /> isn't rendered into the DOM anywhere. Edit: use capital names for components: <NavBar /> Commented Dec 30, 2018 at 23:18
  • @Aurel Bílý I'm using JetBrains' WebStorm to compile and run. I created a new project and chose React App as project type. There's nothing in the console. Commented Dec 30, 2018 at 23:18
  • 1
    Try using github.com/facebook/create-react-app instead Commented Dec 30, 2018 at 23:19
  • 3
    The issue is you need to replace navBar with NavBar in App.js Commented Dec 30, 2018 at 23:23

2 Answers 2

3

In JSX, lower case tags are considered to be simple HTML/SVG elements. You can use lower case only if you use accessors (so with a dot like bla.blabla).

You can read about it here for example.

So in your case you must change the class name navBar to NavBar and then in the render method:

render() {
        return (
            <NavBar/>
        );
    }
Sign up to request clarification or add additional context in comments.

2 Comments

I'm pretty new to JS and had no idea this would make any difference. Thank you very much, it works now!
@pfincent JS doesn't care; React does.
1

Here is a full working example: ** Note: NavBar.js shoud start with a Capital letter.

App.js

import React from "react";
import ReactDOM from "react-dom";
import NavBar from "./components/NavBar";

function App() {
  return (
    <div className="App">
      <NavBar />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

NavBar.js

import React from "react";

export default class NavBar extends React.Component {
  render() {
    return (
      <div>
        <p>Hello world.</p>
      </div>
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.