Skip to main content

React Weather App




First, create a new React project.


Before implementation of any React project

    Spend some time and think about the parts (components) of the application that you can divide into. As an example, I divide this weather app into three components.

  1. Header
  2. Weather Card List
  3. Weather Card

Project File Structure




index.js


import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);



app.js


import './App.css';
import Header from './components/header';
import WeatherCards from './components/weather-cards';

function App() {
  return (
    <div className="App">
      <Header />
      <WeatherCards />
    </div>
  );
}

export default App;



header.js


import React from 'react';

export default function Header() {

    return (
        <h1 className="header">Weather App</h1>
    );
}



weather-cards.js


import React from 'react';
import Data from '../data/weather-data';
import Card from './card';

class WeatherCards extends React.Component {

    state = {
        hideMoreDetails: true
    }

    render() {
        return (
            <div >
                <div className="weather-cards">
                    {Data.map(weather => {
                        return (
                            <Card key={weather.id} weather={weather} hide={this.state.hideMoreDetails} />
                        )
                    })}
                </div>

            </div>
        );
    }
}

export default WeatherCards;



card.js


import React from 'react';

export default function Card(props) {

    function handleClick() {
        alert(props.weather.moreDetails);
    }

    return (
        <div className="card">
            <h3>{props.weather.weekDay}</h3>
            <small>{props.weather.date} {props.weather.year}</small>
            <img src={props.weather.url} alt="weather-image" />
            <h6>{props.weather.temperature} F</h6>
            <small>{props.weather.tempInWords}</small>
            <h5 onClick={handleClick} style={{ cursor: "pointer" }}>More Details</h5>
        </div>
    );
}



React Weather App Code (Github) - https://cb.run/fmaH

React Weather App Video - https://cb.run/jZ0Z


Related:-

React Todo App Video - https://cb.run/2YdN

Comments

Popular posts from this blog

Jquery - Get Input Value And Set Input Value

  HTML input field Jquery

How To Initialise A React Application

  Prerequisites:- Node js installed on PC. Steps:- Create a new folder and open it. Open a command prompt from the current. Create a new React application  - npx create-react-app <app-name> Navigate to application folder - cd <app-name> Run the application - npm start Launches the test runner - npm test Builds the app for production - npm run build React Documentation -  https://reactjs.org/