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

190
Views
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 answers
Answer question

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 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!