When you use React.js, every JavaScript file in your application is invisible to every other JavaScript file by default. ProfilePage.js and NavBar.js can’t see each other.
This is a problem!
On line 9, you just added an instance of the NavBar
component class. But since you’re in ProfilePage.js, JavaScript has no idea what NavBar
means.
If you want to use a variable that’s declared in a different file, such as NavBar
, then you have to import the variable that you want. To import a variable, you can use an import
statement:
import { NavBar } from './NavBar.js';
We’ve used import
before, but not like this! Notice the differences between the above line of code and this familiar line:
import React from 'react';
The first important difference is the curly braces around NavBar
. We’ll get to those soon!
The second important difference involves the contents of the string at the end of the statement: 'react'
vs './NavBar.js'
.
If you use an import
statement, and the string at the end begins with either a dot or a slash, then import
will treat that string as a filepath. import
will follow that filepath, and import the file that it finds.
If your filepath doesn’t have a file extension, then “.js” is assumed. So the above example could be shortened:
import { NavBar } from './NavBar';
One final, important note:
None of this behavior is specific to React! Module systems of independent, importable files are a very popular way to organize code. React’s specific module system comes from ES6. More on all of that later.
Instructions
The <NavBar />
on line 9 isn’t going to work until you import NavBar.js.
In ProfilePage.js, on line 3, import NavBar
from NavBar.js. ProfilePage.js and NavBar.js are located in the same parent directory.