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.ziparchive you downloaded in the prior step.
- Open File Explorer (Windows) or Finder (Mac).
- Copy the
labs\ts\snippets\lab05\assetsdirectory into thekeeptrack\publicdirectory. - Copy the files
labs\ts\snippets\lab05\MockProjects.tsandlabs\ts\snippets\lab05\Project.tsinto thekeeptrack\src\projectsdirectory.
Display the data
Open the file
src\projects\ProjectsPage.tsx.Use
JSON.stringify()to output theMOCK_PROJECTSarray fromMockProjects.tsin therendermethod 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.tsx+ 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