When you use React.js, every JavaScript file in your
application is invisible to every other JavaScript
file by default.
One .js file cannot see another .js file.
For instance, you may have a React Application that contains...
Main.js
NavBar.js
Directions.js
If you want your main.js to use a variable that’s declared in a different file,
such as NavBar.js, then you have to import the variable that you want.
To import a variable, you can use an import statement:
import { Contact } from './NavBar.js';
NOTE
The file extension defaults to .js so you you could just say
import { NavBar } from './NavBar';
and it will know you mean NavBar.js
import React from 'react'; import ReactDOM from 'react-dom'; import {NavBar} from './NavBar.js'; class Main extends React.Component { render() { return ( <div> <NavBar /> <h1>All NavBar Details are above!</h1> <p>...More comments and things are here</p> </div> ); } } ReactDOM.render( <Main />, document.getElementById('app') );The above code will give access to the NavBar.js file by the line
import React from 'react';
export class NavBar extends React.Component {
render() {
const name = ['home', 'blog', 'pics',
'bio', 'art', 'shop',
'about', 'contact'];
const navLinks = pages.map(page => {
return (
<a href={'/' + page}>
{page}
</a>
)
});
return <nav>{navLinks}</nav>
}
}
So the above code creates a class object called NavBar
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="stylesheet" href="./styles.css"> <title>Learn ReactJS</title> </head> <body> <main id="app"></main> <script src="/React/react-course-bundle.min.js" ></script> <script src="/ProfilePage.compiled.js" ></script> </body> </html>...I'll add more about this file when I understand it better...'
In Main.js there is a line (at the bottom) telling it to
render the class (Main), see line...
ReactDOM.render(
<Main />,
document.getElementById('app')
);
This tells the app to update the element that has an id="app"
in this case, in index.html there is an element called <main>
that has the id="app" associated with it.
(although, at this moment I don't know how it knows to look at file index.html
and not a different html file)....UPDATE, apparently the webpack you
load on for React does all this and defaults to index.html
I guess one day I'll look into that more.
In React, as well as Classes we can also define components as JavaScript functions.
We call them function components to differentiate them from class components.
See below for example code...
// A component class // written in the usual way: export class MyComponentClass extends React.Component { render() { return <h1>Hello world</h1> } } // The same component class, //written as a stateless functional component: export const MyComponentClass = () => { return <h1>Hello world</h1> } // The same component class, //written as a NAMED functional component: export function MyComponentClass () { return <h1>Hello world</h1> } // Works the same either way: ReactDOM.render( <MyComponentClass />, document.getElementById('app') );
You can also access the props property but you will not need the this.props
command as it will be treated as a parameter being passed to the function, eg...
import React from 'react'; import ReactDOM from 'react-dom'; export const NewFriend = (props) => { return ( <div> <img src={props.src} /> </div> ); } ReactDOM.render( <NewFriend src="./Images/squid.jpg" />, document.getElementById('app') );
Instead of creating the function and saving it as an object that
contains an anonymous function (like the last pane) you can create it
as an actual NAMED function, eg..."
import React from 'react'; import ReactDOM from 'react-dom'; export function NewFriend (props) { return ( <div> <img src={props.src} /> </div> ); } ReactDOM.render( <NewFriend src="./Images/squid.jpg" />, document.getElementById('app') );