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

  1. Initialize a new Vite + React project:

  2. Clean up default files:

    • Remove unnecessary boilerplate (e.g.src/logo.svg).

    • Create the following structure inside src/:


3. Define Static Data (stockData.js)

In src/data/stockData.js, export an array of stock objects:


4. Create the Stock Component (Stock.jsx)

This component will:

  • Receive a stock object via props

  • Destructure values from stock (stockName, logo, currentPrice, prevClosingPrice)

  • Compute:

    1. numericalChange = currentPrice - prevClosingPrice

    2. rateChange = (numericalChange / prevClosingPrice) * 100

    3. arrow = "⬆" / "⬇" / "▬" based on change sign

    4. 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)

  1. Import your data and component.

  2. Maintain state (currentData) to allow dynamic updates.

  3. Map through currentData, passing each stock as a prop to <Stock />.

  4. (Optional) Simulate live price updates using setInterval inside useEffect.


6. Styling (style.css)

Use the provided CSS to layout the grid, color-code changes, and style your app. Ensure your className attributes match those in the CSS (e.g., .green, .red, .stock-container).


7. Assets

Place your SVG files in src/images/ and reference them with relative paths in stockData.js and App.jsx.


8. Run & Test

  1. Start development server: npm run dev

  2. Open http://localhost:5173 in your browser.

  3. 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).

  4. 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

  1. Initialize prevData with the same data as currentData initially.

  2. Update prevData every time currentData is updated.

Here's the modified App.jsx:

Explanation

  • App.jsx:

    • prevData is initialized with the same data as currentData.

    • Every 4 seconds, prevData is updated to the current currentData before currentData is updated with new random prices.

    • The Stock component is now passed both stock and prevStock as props.

Stock.jsx

  1. Accept prevStock as a prop to get the previous stock data.

  2. Calculate the numericalChange and rateChange using both currentPrice and prevClosingPrice.

Here's the modified Stock.jsx:

  • Stock.jsx:

    • stock property is used to calculate numericalChange and rateChange.

    • prevStock property is used to update prevClosingPrice one step behind currentPrice.

    • The numericalChange and rateChange are displayed to reflect the change in price.

    • Both current and previous prices are shown for reference.

    This setup ensures that prevClosingPrice is always one step behind currentPrice, allowing you to accurately calculate and display the changes.

Last updated