search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

1 answers
Q&A How to update a React component's dependencies in useEffect without creating an infinite loop

0

I have a React component where I need to wait for a stored value (member) to be updated and then delete a member based on a condition before setting the stored value. However, this obviously creates a loop.

useEffect(() => {
    if(members.length > 0) {
        setMembers(members.filter((item) => item.name != 'Bob'));
    }

}, [members]);

Is there any good way to solve this problem? Based on my application setup, I really need to do this in this component.

Your Answer
submit

1 answers
0

You can try using updater state const [update, setUpdate] = useState(false) and pass update as a dependent array. When the member list is updated, use setUpdate(!update).

2024-04-06 09:48:40

submit

Popular tool

vc9-vc14 (32+64 bit) runtime library collection (link below)

vc9-vc14 (32+64 bit) runtime library collection (link below)

Download the collection of runtime libraries required for phpStudy installation

VC9 32-bit

VC9 32-bit

VC9 32-bit phpstudy integrated installation environment runtime library

PHP programmer toolbox full version

PHP programmer toolbox full version

Programmer Toolbox v1.0 PHP Integrated Environment

VC11 32-bit

VC11 32-bit

VC11 32-bit phpstudy integrated installation environment runtime library

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use