UseMemo has no return value
P粉547420474
P粉547420474 2024-02-26 09:23:16
0
1
296

I have the following scenario. I pass an array of objects (houses) to the component. Then I want to loop through it in the useMemo function and display it in the return method. However, I can't see anything. UseMemo is called and runs, but as I said, I don't see any objects

export default function CompactView({ houses }) {

const houseMemo = useMemo(() => {
houses?.map((house) => {
  return (
       <div>
        ...
       </div>              
     );
   });
}, [houses]);

return (
<>
 ...
  <div>
    {houseMemo}
  </div>
</>

 );
}

P粉547420474
P粉547420474

reply all(1)
P粉683665106

The problem you are having is with arrow functions in useMemo. It is missing a return statement. Your code should look like this:

const houseMemo = useMemo(() => {
return houses?.map((house) => {
  return (
       
...
); }); }, [houses]);

Note the return of houses?.map before, or you can change it to:

const houseMemo = React.useMemo(
    () =>
      houses?.map((house) => {
        return 
...
; }), [houses] );

In the second code example, I removed the {} brackets inside the useMemo callback. hope it helps you

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template