Lab 5: Data
Objectives
- Add data
- Display the data
Steps
Add data
- Download the code snippets, data, and images needed for the labs by following these steps.
- Click this link to open the snip repository on GitHub.
- Click the Green Code button then choose Download ZIP.
- Unzip the file
snip-master.zip
archive you downloaded in the prior step.
- Open File Explorer (Windows) or Finder (Mac).
- Copy the
snip-master\labs\js\snippets\lab05\assets
directory (including the assets directory) into thekeeptrack\public
directory. - Copy the files
snip-master\labs\js\snippets\lab05\MockProjects.js
andsnip-master\labs\js\snippets\lab05\Project.js
into thekeeptrack\src\projects
directory.
Display the data
Open the file
src\projects\ProjectsPage.js
.Use
JSON.stringify()
to output theMOCK_PROJECTS
array fromMockProjects.js
in therender
method of the component.TIPS:
- React components can only return one root element so you will need to wrap the
<h1>
and<pre>
tags in a React Fragement<></>
. - Wrapping output in a HTML
<pre></pre>
(preformatted) tag retains whitespace. - To switch to JavaScript in JSX use
{ }
- JSON.stringify(MOCK_PROJECTS, null, ' ')'s third argument is used to insert white space into the output JSON string for readability purposes. The second argument is a replacer function so we can pass null because we don't need to replace anything.
Solution
src\projects\ProjectsPage.js
+ import { MOCK_PROJECTS } from './MockProjects';function ProjectsPage() {- return <h1>Projects</h1>+ return (+ <>+ <h1>Projects</h1>+ <pre>{JSON.stringify(MOCK_PROJECTS, null, ' ')}</pre>+ </>+ );}export default ProjectsPage;- React components can only return one root element so you will need to wrap the