score:1

react hook can be used only functional component whereas componentdidmount is the life cycle method for class based component.

score:2

you can use hooks such as usestate only inside functional component whereas setstate in the lifecycles in class components. you don't wanna mix two. a typical usage would go something like this:

import react, { usestate } from 'react';

function example() {
  const [myval, setmyval] = usestate('hello world');

  return (
    <div>
      <p>{myval}</p>
    </div>
  );
}

read more

if you want to go with lifecycle methods (such as componentdidmount), you may consider using sestate. also, you can't use setstate inside render method as it would trigger an infinite call.


Related Query

More Query from same tag