score:0

usestate in reactjs is asynchronous, thus it takes certain milli seconds to update before which the component would have already been rendered. one approach i found working was to directly use the value returned from the methods rather than setstate.

<div classname="cardpage__favourite" onclick={togglefav}>
   {checkfav() ? <i classname={`pi pi-star-fill cardpage__star`} /> :
    <i classname={`pi pi-star cardpage__star`} />}
</div>

also once you set the isfav using setisfav, you can use its value in other methods using the state variable isfav.

score:0

if (icons[i].id.tostring() === itemid) {

converting the id to tostring() will help you solve your query since you're also checking for type using ===.


Related Query

More Query from same tag