score:0

it sounds like anothercomponent does not take into consideration the changed prop - this should be considered to be a bug in anothercomponent. ideally, you'd fix it so that the changed prop gets used properly. eg, just for an example, maybe it's doing

const [searchhandler, setsearchhandler] = usestate(props.onsearch);

and failing to observe prop changes as it should. or, for another random example, this could happen if the listener prop gets passed to an addeventlistener when the component mounts but again doesn't get checked for changes and removed/reattached.

if you can't fix anothercomponent, you can use a ref for mylist in addition to the prop:

const mycomponent = ({ mylist }) => {
    const mylistref = useref(mylist);
    useeffect(() => {
        mylistref.current = mylist;
        setfilteredlist(mylist);
    }, [mylist]);
    const [filteredlist, setfilteredlist] = usestate(mylist);
    const onsearchhandler = (searchtext) => {
        const filtereditems = mylistref.current.filter(item =>
            item.name.tolowercase().includes(searchtext.tolowercase())
        );
        setfilteredlist(filtereditems);
    };

it's ugly, but it might be your only option here.


Related Query

More Query from same tag