I don't know if this is possible (or recommended), but I'm looking for a way to trigger some code, much like how an event handler triggers a function call. Something like this:
``` const Context = createContext(null); function Parent() { const onSave = async () => { const res = await someMutationFunction(); if (res.success) { // trigger child component } } return ( <Context.Provider value={// what to put in here?}> <Child /> </Context.Provider> ) }
function Child() { const context = useContext(Context);
// context should trigger this effect useEffect(() => { }, [context]); } ```
Right now I have a boolean that flips to true, then back to false in 100 ms:
const [isUpdated, setIsUpdated] = useState(false);
if (res.success) {
setIsUpdated(true);
setTimeout(() => setIsUpdated(false), 100);
}
But is there a better way of doing this?
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/react/comme...