A
A
Artem Vladilenko2020-11-16 18:19:41
React
Artem Vladilenko, 2020-11-16 18:19:41

Why doesn't setState change the state?

Hello. Can you correct me? My setState does not change the state (city), why?
Perhaps I made a mistake due to inattention or due to lack of knowledge :))))

function App(props) {

  let state = {
    city: "Piter"
  }

  const gettingWeather = async (e) => {
    e.preventDefault();
    let city = e.target.elements.city.value;
    const api_url = await
    fetch(`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${API_KEY}&units=metric`);
    const date = await api_url.json();
    console.log(date)

    const handleClick = () => {
      this.setState({
        city: "Moscow"
      })
    }
    console.log(state.city)
  };

  return(
    <>
      <Info />
       <Form weatherMethod={gettingWeather} />
      <Weather city={state.city} />
    </>
  )
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Muravyov, 2020-11-16
@ChacNoris

The matter is that you thrust setState in function which you do not cause.
Better look at hooks. They are much easier to deal with than classes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question