Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda