Level 2 Team Project
Stock Tracker App
This guide provides a step-by-step procedure to build a Stock Tracker application using React and Vite, with dynamically-generated stock data. College students can follow along to implement and understand each part of the app.

1. Prerequisites
Node.js and npm installed on your machine.
Basic knowledge of React (components, props, state, hooks).
A code editor (e.g., VS Code).
2. Project Setup
Initialize a new Vite + React project:
Clean up default files:
Remove unnecessary boilerplate (e.g.
src/logo.svg).Create the following structure inside
src/:
3. Define Static Data (stockData.js)
stockData.js)In src/data/stockData.js, export an array of stock objects:
4. Create the Stock Component (Stock.jsx)
Stock Component (Stock.jsx)This component will:
Receive a
stockobject via propsDestructure values from
stock(stockName, logo, currentPrice, prevClosingPrice)Compute:
numericalChange =
currentPrice - prevClosingPricerateChange =
(numericalChange / prevClosingPrice) * 100arrow = "⬆" / "⬇" / "▬" based on change sign
colorClass = "green" / "red" /
undefined
Format both change values to two decimal places using
.toFixed(2)
Note: Using
.toFixed(2)ensures all numbers are represented with two decimal places, e.g.,5→5.00,52.1→52.10.
5. Update the Main App (App.jsx)
Import your data and component.
Maintain state (
currentData) to allow dynamic updates.Map through
currentData, passing eachstockas a prop to<Stock />.(Optional) Simulate live price updates using
setIntervalinsideuseEffect.
6. Styling (style.css)Use the provided CSS to layout the grid, color-code changes, and style your app. Ensure your
classNameattributes match those in the CSS (e.g.,.green,.red,.stock-container).
7. AssetsPlace your SVG files in
src/images/and reference them with relative paths instockData.jsandApp.jsx.

8. Run & Test
Start development server:
npm run devOpen
http://localhost:5173in your browser.Verify:
Stock cards render dynamically from
stockData.js.Numeric and percentage changes update correctly.
Color and arrows reflect gains, losses, or no change.
Live price updates (if implemented).
Output:

9. Improve App
We noted that prevClosingPrice is not updated at every 4 sec as currentPrice is updated. Let's improve the code by modifying App.jsx and Stock.jsx to reflect the change accordingly.
To ensure that prevClosingPrice is properly synchronized with currentPrice and can be updated accordingly, you need to modify both App.jsx and Stock.jsx. Here's how you can do it:
App.jsx
Initialize
prevDatawith the same data ascurrentDatainitially.Update
prevDataevery timecurrentDatais updated.
Here's the modified App.jsx:
Explanation
App.jsx:
prevDatais initialized with the same data ascurrentData.Every 4 seconds,
prevDatais updated to the currentcurrentDatabeforecurrentDatais updated with new random prices.The
Stockcomponent is now passed bothstockandprevStockas props.
Stock.jsx
Accept
prevStockas a prop to get the previous stock data.Calculate the
numericalChangeandrateChangeusing bothcurrentPriceandprevClosingPrice.
Here's the modified Stock.jsx:
Stock.jsx:
stockproperty is used to calculatenumericalChangeandrateChange.prevStockproperty is used to updateprevClosingPriceone step behindcurrentPrice.The
numericalChangeandrateChangeare displayed to reflect the change in price.Both current and previous prices are shown for reference.
This setup ensures that
prevClosingPriceis always one step behindcurrentPrice, allowing you to accurately calculate and display the changes.
Last updated