0

The example code is here:

import Button from '@mui/material/Button';
import { memo } from 'react';

function PokemonBriefCard(props) {

  return (
    <div>Some random component</div>
  );
}

export default memo(PokemonBriefCard);
// export default PokemonBriefCard;

The usage of that component is the following:

          <div className="card-brief-container">
            {
              pokemonList.map((pokemon, index) => 
              <div key={index}>
                {PokemonBriefCard({
                  ...pokemon,
                  action: pokemon.favourite ? removeFav : addFav
                })}
              </div>
              )
            }
          </div>

It seems that if I put memo to the component, it would throw an error like this:

Error Message

And once I remove the memo (like using the export default PokemonBriefCard instead), the project would render without an issue.

Am I using React.memo in the wrong way? I read through the introduction doc but can't find the cause of it.

4
  • Your code snippet is correct. Commented Dec 5, 2023 at 8:58
  • @Anshu But I am still getting this error. Any ideas? Commented Dec 5, 2023 at 9:41
  • 1
    How do you use PokemonBriefCard ? Commented Dec 5, 2023 at 10:15
  • @OktayYuzcan Thanks for noting, just added the usage to the question. Commented Dec 5, 2023 at 10:17

1 Answer 1

3

React.memo is used for components. If PokemonBriefCard is a component, you should not call it as function

 PokemonBriefCard({   // this is wrong
     ...pokemon,
     action: pokemon.favourite ? removeFav : addFav
 })

You should use it as a component: <PokemonBriefCard ... />

If it is just a render function PokemonBriefCard(...) (better make the name camel case), then you can't use React.memo, since it is used only on components.

Note: React.memo does not return function, this is why the error tells you it is not a function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.