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

116
Views
How to make a search box working properly on react js

I am a beginner and I am learning React JS. I am doing a basic project in React. I have a search box. When I search for something my page will show search related content.

Here I used includes() to make my search workable. But it is not working.

I have indicated the line of setting a new array of dogs at line 40. Pleae have a look to my code.

Here is my codes:

Home.jsx

import React, { useState, useEffect } from 'react';
import Dogs from './components/Dogs/Dogs';
import Search from './components/search/Search';

const Home = () => {
  const [error, setError] = useState(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [value, setValue] = useState('');
  const [search, setSearch] = useState('');
  const [dogs, setDogs] = useState([]);

  useEffect(() => {
    fetch('https://api.thedogapi.com/v1/breeds', {
      headers: {
        Authorization: '4139956d-7014-4fa8-b295-8e361418c807',
      },
    })
      .then(response => response.json())
      .then(
        data => {
          setIsLoaded(true);
          setDogs(data);
        },
        error => {
          setIsLoaded(true);
          setError(error);
        }
      );
  }, []);

  const handleChange = event => {
    setValue(event.target.value);
  };

  const handleSearch = () => {
    setSearch(value);
    setValue('');
  };

  // This is the function to making a new array of dogs of searched name
  useEffect(() => {
    const newDogList = dogs.filter(dog => {
      if (dog.name.includes(search)) dog
    });
    setDogs(newDogList);
  }, [search]);

  if (error) {
    return <div>Error: {error.message}</div>;
  } else if (!isLoaded) {
    return <div>Loading...</div>;
  } else {
    return (
      <div className="container">
        <Search
          value={value}
          handleChange={handleChange}
          handleSearch={handleSearch}
        />
        <Dogs dogs={dogs} />
      </div>
    );
  }
};

export default Home;


How can I make it workable?

about 4 years ago · Juan Pablo Isaza
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!