Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
fetch call firing onChange rather than onSubmit

I'm building a recipe search page with React. There are two components - Navbar and Home. The Navbar takes in the search query and the Home renders the data.

The API call works fine and the recipes render as needed. The issue I'm having is whenever the user types anything in the search bar, my fetch call fires - but I need the request to fire only when the submit button is pressed.

Here's the code: (I'm using App.js as the intermediary between the two components because they're siblings)

NAVBAR

function Navbar({ setInput, handleSubmit }) {

  return (
  // Step 1 - User inputs query
     <form onSubmit={handleSubmit}>
       <input onChange={(e) => setInput(e.target.value)} />
       <button type="submit">SUBMIT</button>
     </form>
  )
}

APP.JS

function App() {
  const [input, setInput] = useState('')

  const handleSubmit = (e) => {
    e.preventDefault()
    navigate('/home')
  };

  return (
  // Step 2 - App handles exchange
    <Navbar setInput={setInput} handleSubmit={handleSubmit} />
    <Home input={input} handleSubmit={handleSubmit} />
  )
}

HOME

function Home({ input, handleSubmit }) {

useEffect(() => {
        axios.get(`www.recipeAPI/${input}`)
        .then(res => //render recipes with res)
    }, [handleSubmit]);


  return (
    // Step 3 - fetch call data gets rendered
  )
}

I've tried all kinds of hooks and listeners but nothing seems to stop the API from calling everytime a user types.

I appreciate the help in advance :/

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Whenever App is re-rendered, stating input state for example, you re-create the handleSubmit function. Since the handleSubmit function is a dependency of the useEffect in home, it react to the change, and a call is issued.

To prevent that wrap handleSubmit with useCallback, which would memoize the function, and prevent generating a new function on each re-render.

function App() {
  const [input, setInput] = useState('')

  const handleSubmit = useCallback((e) => {
    e.preventDefault()
    navigate('/home')
  }, []);

  return (
  // Step 2 - App handles exchange
    <Navbar setInput={setInput} handleSubmit={handleSubmit} />
    <Home input={input} handleSubmit={handleSubmit} />
  )
}
about 4 years ago · Santiago Gelvez Report

0

What I ended up doing was moving the entire fetch call to App.js and passing down the results to home. That solved the issue

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!