I'm trying to run a function on a context
using useEffect
, but after the component mounts. Otherwise the function runs as the context is first loaded in, and gives a bad result. I'd love to nest a useEffect
inside another useEffect
that runs after load, but I cant. How do I trigger the useEffect
to wait until componentDidLoad to run? Do I need to revert my functional component to a class function?
Here is the effect im running
useEffect(() => {
let count = Object.keys(context).filter((x) => context[x] !== "").length;
numChange = count;
}, [context]); <=== run AFTER the original update of the context
To clarify. The context is an object, and I count how many of the objects properties have values. As the context is initially counted (let's say 5 out of the 10 properties already had values), the "number === 5". But what I want to do is count the changes after the initial amount of properties with values in the context when the component mounts. So if n = 5, I want to count any property that gets a value after n.