-1

I want to call a vanillaJS function after the component render so it can run the code but it always run before the component loads and gives me errors that it cannot find the specific documents

for example how to run this code after the component fully rendered ?

function runningAfter() { console.log("called after page loads")}

I searched and found that there is componentDidmount but i am using the latest react version and i cannot find this method

Razor Jhon
  • 115
  • 1
  • 12

1 Answers1

0

If you're using a function component, you'll need useLayoutEffect with an empty dependency list:

function MyComponent() {
  useLayoutEffect(() => {
    console.log("called after component rendered")
  }, []);
  return <div>Hello!</div>;
}
AKX
  • 152,115
  • 15
  • 115
  • 172