Level 1 Team Project
Weather Minotoring App

Weather Monitoring App: Setup and Explanation
This guide explains how to build a simple React weather application using Vite. It displays weather information for different days and changes the background based on the weather condition.
1. Project Setup (Using Vite)
Install Node.js: Make sure you have Node.js (which includes npm) installed on your system. You can download it from https://nodejs.org/.
Create Vite Project: Open your terminal or command prompt and run the following command to create a new React project named
weather-app:Navigate to Project Directory:
Install Dependencies:
Run the Development Server:
This will start the development server, and you can view the basic Vite React app in your browser (usually at
http://localhost:5173).
2. Project Structure
Organize your project files as follows within the src directory:
src/components/: This directory will hold reusable UI components.Weather.jsx: The component responsible for displaying the weather details for a specific day.weatherData.js: A JavaScript file to store the hardcoded weather data array.
src/images/: Place your background images here (e.g.,sunny-background.jpg,cloudy-background.jpg, etc.). Important: You need to provide these image files yourself.src/App.jsx: The main application component where the state and logic for switching days will reside.src/style.css: Your custom CSS rules for styling the application.
3. Core Concepts Explained
React Components: React apps are built using components – reusable pieces of UI. We'll have
App(the main component) andWeather(a child component).JSX: A syntax extension for JavaScript that looks like HTML. It's used to describe what the UI should look like.
State (useState Hook):State allows components to "remember" information and re-render when that information changes. We'll use state inApp.jsxto keep track of which day's weather is currently displayed.const [currentDayIndex, setCurrentDayIndex] = useState(0);initializes a state variablecurrentDayIndexto0and provides a functionsetCurrentDayIndexto update it.
Props: How parent components pass data down to child components.
App.jsxwill pass the weather data for the current day down to theWeather.jsxcomponent as props.Event Handling (onClick):We'll attach anonClickhandler to the button. When clicked, it will call a function to update thecurrentDayIndexstate, causing the app to display the next day's weather.Conditional Rendering/Styling: The background image will change based on the
conditionproperty of the current day's weather data. We achieve this by dynamically setting a class or inline style on the main container element inApp.jsx.Modularity: Separating concerns into different files (
weatherData.js,Weather.jsx,App.jsx,style.css) makes the code easier to understand, manage, and maintain.CSS Styling: The
style.cssfile contains rules to style the HTML elements generated by the React components (e.g., centering content, styling the button, setting text colors).
4. Data Flow
weatherData.jsexports the array of weather objects.App.jsximportsweatherData.App.jsxusesuseStateto managecurrentDayIndex.App.jsxselects the weather data for thecurrentDayIndexfrom theweatherDataarray.App.jsxdetermines the correct background image URL based on the selected day's weathercondition.App.jsxpasses the selected day's weather data (day,condition,emoji,lowTemp,highTemp) as props to theWeather.jsxcomponent.App.jsxrenders theWeathercomponent and the "Next Day" button.Weather.jsxreceives the props and displays the weather information.When the button in
App.jsxis clicked, thehandleNextDayClickfunction updates thecurrentDayIndexstate.React detects the state change and re-renders
App.jsx, which in turn re-rendersWeather.jsxwith the new day's data and potentially updates the background image.
Implement the App
Step 1. Create the files: Create the directories (src/components, src/images) and files (src/components/weatherData.js, src/components/Weather.jsx, src/style.css) as shown in the structure.
Step 2. Copy the code: Paste the corresponding code into each file (weatherData.js, style.css, Weather.jsx, App.jsx).
Step 3. Add Images: Place your background images (e.g., sunny-background.jpg, rainy-background.jpg, snowy-background.jpg) inside the src/images folder. Make sure the filenames match those used in weatherData.js.
Step 4. Run the app: If the development server isn't running, start it with npm run dev in your terminal (from the weather-app directory).
Step 5. View: Open your browser to the specified local address (e.g., http://localhost:5173).
Last updated