Bachi
Bachi

Reputation: 111

React How to use state in different component

Somebody know why my state doesn't update This is my Context:

import React, { createContext, useState } from 'react';

export const WeatherDataContext = createContext();

const WeatherDataContextProvider = (props) => {
    const [weather, setWeather] = useState(
        {
            city: null,
            temp: null
        }
    )
    const addWeather = (city, temp) => {
        setWeather({
            city,
            temp
        })
    }
    
    return (
        <WeatherDataContext.Provider value={{weather, addWeather}}>
            {props.children}
        </WeatherDataContext.Provider>
    )
}

export default WeatherDataContextProvider

And My Form where I pass my data from axios to addWeather function:

import React, {useContext, useState} from 'react';
import { WeatherDataContext } from '../context/WeatherDataContext';
import axios from 'axios'
import {Link} from 'react-router-dom'


const WeatherForm = () => {
    const {addWeather} = useContext(WeatherDataContext);
    const [value, setValue] = useState('')

    const handleChange = (e) => {
        e.preventDefault();
        axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${value}&appid=${KEY}&units=metric`)
            .then(res => {
                
                addWeather({
                    city: res.data.name,
                    temp: res.data.main.temp
               });
                
            })
        
    }


    return (
        <div class='weather-form'>
            <form onSubmit={handleChange}>
                <input placeholder='Wpisz miasto' onChange={(e) => setValue(e.target.value)} value={value} required/>
                <Link to='/weather'><button>Wyszukaj</button></Link>
                
            </form>
        </div>
    )
}

export default WeatherForm

After i update my state in the context i want to use that data in different component like this:

import React, {useContext, useState} from 'react';
import { WeatherDataContext } from '../context/WeatherDataContext';

const WeatherFront = () => {
    const {weather} = useContext(WeatherDataContext)
    
    console.log(weather)
    return (
        <div class='weather-front'>
            <h1>City: {weather.city}, Temperatura: {weather.temp}</h1>
        </div>
    )
}

export default WeatherFront

Sum up i dont know why but my state doesnt update and my weather component return only null from initial state

import React from 'react';
import WeatherDataContextProvider from '../context/WeatherDataContext'
import WeatherFront from '../components/WeatherFront';


const Weather = () => {
    return (
        <div class='weather'>
            <WeatherDataContextProvider>
                <WeatherFront />
            </WeatherDataContextProvider>
        </div>
    )
}

export default Weather

Upvotes: 1

Views: 63

Answers (1)

Shubham Khatri
Shubham Khatri

Reputation: 282000

WeatherDataContext has the values as weather and addWeather and not changeCity and changeWeather

You need to consume it appropriately

const {addWeather} = useContext(WeatherDataContext);
const handleChange = (e) => {
        e.preventDefault();
        axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${value}&appid=${KEY}&units=metric`)
            .then(res => {
                
                addWeather({
                     city: res.data.name,
                     temp: res.data.main.temp
                });
            })
        
    }

Upvotes: 1

Related Questions