# React: State wont change when I search for different query

**URL:** <https://forum.freecodecamp.org/t/react-state-wont-change-when-i-search-for-different-query/351072>\
**Category:** General\
**Created:** [February 18, 2020, 1:19am UTC](https://forum.freecodecamp.org/t/react-state-wont-change-when-i-search-for-different-query/351072 "2020-02-18T01:19:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkhan698](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/fkhan698/32/133991_2.png) [@fkhan698](https://forum.freecodecamp.org/u/fkhan698)\
**Post date:** [February 18, 2020, 1:19am UTC](https://forum.freecodecamp.org/t/react-state-wont-change-when-i-search-for-different-query/351072/1 "2020-02-18T01:19:26Z")

</div>

I am using the Recipe DB API for a [Recipe app](https://codesandbox.io/s/hardcore-bartik-k49bb) I am building. So far I have the state of the query set to chicken which returns all the chicken recipes from the API. But whenever I look up something different, say ‘banana’, the state doesn’t change. I moved my `getRecipes` function inside the `useEffect` which got rid of the error message saying that `useEffect` was missing a dependency. However, I still can’t get it to work. Here is my code so far:

```auto
import React, { useEffect, useState } from "react";
import Recipe from "./Recipe.js";
import "./styles.css";

export default function App() {
  const API_ID = "c38daf94";
  const API_KEY = "850d468a3e994692691631c7c259406c";

  const [recipes, setRecipes] = useState([]);
  const [search, setSearch] = useState("");
  const [query, setQuery] = useState('chicken');

  useEffect(() => {
    async function getRecipes() {
      const response = await fetch(
        `https://api.edamam.com/search?q=${query}&app_id=${API_ID}&app_key=${API_KEY}`
      );
      const data = await response.json();
      setRecipes(data.hits);
      console.log(data.hits);
    }
    if (query !== "") getRecipes();
  }, [query]);

  // const getRecipes = async () => {
  // const response = await fetch(
  // `https://api.edamam.com/search?q=${query}&app_id=${API_ID}&app_key=${API_KEY}`
  // );
  // const data = await response.json();
  // setRecipes(data.hits)
  // console.log(data.hits);

  // };

  const updateRecipes = e => {
    setSearch(e.target.value);
  };

  const getSearch = e => {
    e.preventDefault();
    console.log(query)
    setQuery(query);
  };

  return (
    <div className="App">
      <input
        className="input"
        onChange={updateRecipes}
        value={search}
        type="text"
      />
      <button className="search" onClick={getSearch}>
        Search
      </button>
      <div>
        {recipes.map(recipe => (
          <Recipe
            key={recipe.recipe.uri}
            title={recipe.recipe.label}
            calories={recipe.recipe.calories}
            image={recipe.recipe.image}
          />
        ))}
      </div>
    </div>
  );
}

```

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [February 18, 2020, 1:43am UTC](https://forum.freecodecamp.org/t/react-state-wont-change-when-i-search-for-different-query/351072/2 "2020-02-18T01:43:52Z")

</div>

I think the root of you problem is here:

```auto
  const getSearch = e => {
    e.preventDefault();
    setQuery(query);
  };

```

You’re setting _query_ to itself. I think you want to set it to _search_.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:35pm UTC](https://forum.freecodecamp.org/t/react-state-wont-change-when-i-search-for-different-query/351072/3 "2021-06-01T19:35:29Z")

</div>


