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

196
Visualizações
State not updating until I add or remove a console log
const BankSearch = ({ banks, searchCategory, setFilteredBanks }) => {
  const [searchString, setSearchString] = useState();

  const searchBanks = (search) => {
    const filteredBanks = [];
    banks.forEach((bank) => {
      if (bank[searchCategory].toLowerCase().includes(search.toLowerCase())) {
        console.log(bank[searchCategory].toLowerCase());
        filteredBanks.push(bank);
      }
    });

    setFilteredBanks(filteredBanks);
  };
  const debounceSearch = useCallback(_debounce(searchBanks, 500), []);
  useEffect(() => {
    if (searchString?.length) {
      debounceSearch(searchString);
    } else setFilteredBanks([]);
  }, [searchString, searchCategory]);

  const handleSearch = (e) => {
    setSearchString(e.target.value);
  };

  return (
    <div className='flex'>
      <Input placeholder='Bank Search' onChange={handleSearch} />
    </div>
  );
};

export default BankSearch;

filteredBanks state is not updating

banks is a grandparent state which has a lot of objects, similar to that is filteredBanks whose set method is being called here which is setFilteredBanks

if I add a console log and save or remove it the state updates

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Adding or removing the console statement and saving the file, renders the function again, the internal function's state is updated returned with the (setState) callback.

(@vnm) Adding filteredBanks to your dependency array won't do much because it is part of the lexical scope of the function searchBanks

I'm not entirely sure of the total context of this BankSearch or what it should be. What I do see is that there are some antipatterns and missing dependencies.

Try this:

export default function BankSearch({ banks, searchCategory, setFilteredBanks }) {
const [searchString, setSearchString] = useState();

const searchBanks = useCallback(
    search => {
        const filteredBanks = [];

        banks.forEach(bank => {
            if (bank[searchCategory].toLowerCase().includes(search.toLowerCase())) {
                filteredBanks.push(bank);
            }
        });

        setFilteredBanks(filteredBanks);
    },
    [banks, searchCategory, setFilteredBanks]
);

const debounceSearch = useCallback(() => _debounce(searchBanks, 500), [searchBanks]);

useEffect(() => {
    if (searchString?.length) {
        debounceSearch(searchString);
    } else setFilteredBanks([]);
}, [searchString, searchCategory, setFilteredBanks, debounceSearch]);

const handleSearch = e => {
    setSearchString(e.target.value);
};

return (
    <div className="flex">
        <Input placeholder="Bank Search" onChange={handleSearch} />
    </div>
)}

It feels like the component should be a faily simple search and filter and it seems overly complicated for what it needs to do.

Again, I don't know the full context, however, I'd look into the compont architecture/structuring of the app and state.

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