Lab 4: Your First Component
Objectives
- Create a component
- Render the component
Steps
Create a component
Create the directory
src\projects
.Create the file
src\projects\ProjectsPage.js
In the file, create a function component that returns the following html:
<h1>Projects</h1>
The solution code for the component appears next. Challenge yourself to write it from scratch before looking at it.
In VS Code, you could use this extension VS Code ES7 React/Redux/React-Native/JS snippets that you installed as part of the setup for the course and then type
rfce
thentab
.Solution
src\projects\ProjectsPage.js
import React from 'react';
function ProjectsPage() {
return <h1>Projects</h1>;
}
export default ProjectsPage;The import:
import React from 'react';
is not required in the latest version(s) of React because it uses a newJSX Transform
.React 17
RC and higher supports the new JSX Transform, and they’ve also releasedReact 16.14.0
,React 15.7.0
, andReact 0.14.10
for people who are still on the older major versions).- With the new JSX Transform, the import statement is only needed at the entry point of the application which is
src\index.js
in a Create React App. Note that the code still works if you include the import in other files but it is no longer required. - The import is included throughout the labs so the code continues to work on older versions of React
Render the component
Remove the
<blockquote>...</blockquote>
we returned in the last lab and replace it with<ProjectsPage/>
wrapped in adiv
with a cssclass
ofcontainer
.src\App.js
+ import ProjectsPage from './projects/ProjectsPage';
function App() {
- return (
- <blockquote cite="Benjamin Franklin">
- Tell me and I forget, teach me and I may remember, involve me and I learn.
- </blockquote>
- );
+ return (
+ <div className="container">
+ <ProjectsPage />
+ </div>
+ );
}Verify the following is displayed in the browser:
Projects