<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Azongo Ismail</title>
    <description>The latest articles on DEV Community by Azongo Ismail (@azongo123).</description>
    <link>https://dev.to/azongo123</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1908675%2F68e0922c-f585-471b-8fc9-a4e1113c53d2.png</url>
      <title>DEV Community: Azongo Ismail</title>
      <link>https://dev.to/azongo123</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/azongo123"/>
    <language>en</language>
    <item>
      <title>How to receive data in form from another component</title>
      <dc:creator>Azongo Ismail</dc:creator>
      <pubDate>Tue, 14 Jan 2025 13:19:47 +0000</pubDate>
      <link>https://dev.to/azongo123/how-to-receive-data-in-form-from-another-component-4i8h</link>
      <guid>https://dev.to/azongo123/how-to-receive-data-in-form-from-another-component-4i8h</guid>
      <description>&lt;p&gt;Hello everyone, I'm building a react weather app where users are allowed to enter their city name and the weather info is displayed back to the user.&lt;/p&gt;

&lt;p&gt;The problem is, I've got the input and button in a form, and I want to receive data from another &lt;code&gt;component&lt;/code&gt; called WeatherApp. &lt;/p&gt;

&lt;p&gt;how do I link the two &lt;code&gt;components&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the form&lt;code&gt;component&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import '../App.css';
import Input from './Input';
import Button from './Button';
import { useState } from 'react';

const Form = ({handleQueryChange}) =&amp;gt; {
    const [city, setCity] = useState('')

    const handleChange = (e) =&amp;gt; {
        setCity(e.target.value)

    }

    const handleSubmit  = (e) =&amp;gt; {
        e.preventDefault();
        handleQueryChange(city);

    }

    return (  
    &amp;lt;div&amp;gt;
        &amp;lt;form className="inputForm" onSubmit={handleSubmit}&amp;gt;
       &amp;lt;Input value={city} onChange={handleChange} /&amp;gt;
       &amp;lt;Button/&amp;gt;
        &amp;lt;/form&amp;gt;

    &amp;lt;/div&amp;gt;);
}

export default Form;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the weatherapp&lt;code&gt;component&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import { BrowserRouter,  Routes,Route } from 'react-router-dom';
import { useState, useEffect } from 'react';
import { WiDaySunny,WiCloud, WiNightCloudy,WiHumidity, WiNightClear } from 'react-icons/wi';
import Layout from './Layout';
import Home from './Home';
import Minutecast from './Minutecast';
import Hourly from './Hourly';
import Today from './Today';
import '../App.css';


const WeatherApp = () =&amp;gt; {
const [data, setData] = useState([]);
const [query, setQuery] = useState('Accra');

const handleQueryChange = (data) =&amp;gt; {
    setQuery(data.location.name);

}

const getWeatherIcons = (description) =&amp;gt;{
    switch(description.toLowerCase()) {
        case 'sunny':
            return &amp;lt;WiDaySunny size='100px'/&amp;gt;;
        case 'cloud':
                return &amp;lt;WiCloud  size='100px'/&amp;gt;;
        case 'clear':
                return &amp;lt;WiNightClear size='100px'/&amp;gt;;
        case 'partly cloudy':

                return &amp;lt;WiNightCloudy size='150px' color='blue' /&amp;gt;;
        default:
                return &amp;lt;WiDaySunny color='red' size='100px' /&amp;gt;;

    }
}

const convertToWhole = (tem) =&amp;gt;{
  const whole = Math.trunc(tem);
  return whole;


}

useEffect(()=&amp;gt;{

const fetchWeatherData = async () =&amp;gt; {
  try {
    const response = await fetch(`https://api.weatherapi.com/v1/forecast.json?key=95f0ddb3a06a4a358cf223933242311&amp;amp;q=${query}&amp;amp;days=10&amp;amp;aqi=yes&amp;amp;alerts=no`);
    const result = await response.json();
    setData(result);

  console.log(result);
  } catch (error) {
    console.error("Error fetching weather data:", error);
  }

};
fetchWeatherData();
}, [data]);



    return ( &amp;lt;&amp;gt;
    &amp;lt;div className="formContainer"&amp;gt;
    {/* &amp;lt;Form onSubmit={handleQueryChange} /&amp;gt; */}
    &amp;lt;/div&amp;gt;
    &amp;lt;BrowserRouter&amp;gt;
    &amp;lt;Routes&amp;gt;
       &amp;lt;Route path ='/'  element={&amp;lt; Layout /&amp;gt;}&amp;gt;
       &amp;lt;Route index element={&amp;lt;Home  data={data} getWeatherIcons={getWeatherIcons} convertToWhole={convertToWhole}/&amp;gt;} /&amp;gt;
       &amp;lt;Route path='minutecast' element={&amp;lt;Minutecast /&amp;gt;}/&amp;gt;
       &amp;lt;Route path='hourly' element={&amp;lt;Hourly data={data} convertToWhole={convertToWhole}/&amp;gt;}/&amp;gt;
       &amp;lt;Route path='today'element={&amp;lt;Today data={data} /&amp;gt;}/&amp;gt;
     &amp;lt;/Route&amp;gt;
     &amp;lt;/Routes&amp;gt;
   &amp;lt;/BrowserRouter&amp;gt;

    &amp;lt;/&amp;gt;
    )

}

export default WeatherApp;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>webcomponents</category>
    </item>
  </channel>
</rss>
