score:2

Accepted answer

short answer

  • using usememo without dependency array will do nothing.
  • generally speaking, don't use usememo to define components.
  • to make sure you are using usememo correctly, set up your linter like here

long answer:

a couple of things first:

note that usememo can only be used within another component or hook (see here) so i'll assume that for the rest of the answer.

i'd say your usage of usememo is incorrect. if you want to define a component with it (however you probably shouldn't) it could be like this:

  const mycomp = usememo(
    () => (props) => (
      <h1>
        {props.text}
      </h1>
    ),[]
  );

this defines a functional component and redefines it whenever the dependencies change. (here the dependencies are [], so it will be defined only once). note that using usememo without a dependency array will result in a linting error (react-hooks/exhaustive-deps) if you set up your linting correctly as suggested here.

if you insist on leaving out the dependency array, usememo will do nothing.

to play around a little bit with different scenarios, take a look at this codesandbox

  const mycompa = (props) => (
    <h1>
      {props.text}
      {counter}
    </h1>
  );

  // equivalent to mycompa. gives linting error
  const mycompb = usememo(() => (props) => (
    <h1>
      {props.text}
      {counter}
    </h1>
  ));

  // will only be defined once and not notice changes to counter. gives linting error
  const mycompc = usememo(
    () => (props) => (
      <h1>
        {props.text}
        {counter}
      </h1>
    ),
    []
  );

  // will notice changes to counter. but generally, this  is not
  // much better than mycompa
  const mycompd = usememo(
    () => (props) => (
      <h1>
        {props.text}
        {counter}
      </h1>
    ),
    [counter]
  );

mycompa and mycompb do the same thing, because mycompb uses usememo without dependencies. mycompc uses that state counter but doesn't recognize changes, because it's not in the dependency array. mycompc recognizes that changes, but overall is not much better than mycompa.

note the whole example is for "playing-around purpose" only so we can inspect how usememo behaves. generally speaking, defining components with usememo is probably not the way to go. for that example, better use a regular component that takes both text and counter as props.

score:1

usememo doesn't affect function calls and the way a component reacts to prop changes in this case. this could be achieved with usecallback but it's not recommended; this is what memo component is for.

considering that both are defined inside component function, the one without usememo will be a new component on each render and so will be remounted, while the one with usememo will behave as expected.

since the component doesn't rely on the scope in which it's defined (it's not a good practice), it shouldn't be defined inside another component and therefore won't benefit from the use of usememo.


Related Query

More Query from same tag