如何在具有命名导出的文件中使用React.memo()?

huangapple go评论116阅读模式
英文:

How to use React.memo() in a file with named exports?

问题

我习惯于使用 export default React.memo(SomeComponent); 这样 React 可以对我的组件进行记忆。

但在一个使用命名导出的文件中,我该如何使用 React.memo()

export { React.memo(MainPostTopic), React.memo(MainPostSubtopic) };

上面的代码行不起作用。

英文:

I'm used to export default React.memo(SomeComponent); so React can memoize my components.

But in a file where I'm using named exports. How can I use React.memo() ?

export { React.memo(MainPostTopic), React.memo(MainPostSubtopic) };

The line above doesn't work.

EXAMPLE FILE

function MainPostTopic(props) {
  console.log('Rendering MainPostTopic...');
  return (
    <LS.Topic_H2 id={props.id}>
      {props.value}
    </LS.Topic_H2>
  );
}

function MainPostSubtopic(props) {
  console.log('Rendering MainPostSubtopic...');
  return (
    <LS.Subtopic_H3>
      {props.value}
    </LS.Subtopic_H3>
  );
}

export { MainPostTopic, MainPostSubtopic };

答案1

得分: 26

这是我最终做的:

const MainPostTopic = React.memo(
  function MainPostTopic(props) {
    console.log('渲染 MainPostTopic...');
    return (
      ...
    );
  }
);

const MainPostSubtopic = React.memo(
  function MainPostSubtopic(props) {
    console.log('渲染 MainPostSubtopic...');
    return (
      ...
    );
  }
);

export { MainPostTopic, MainPostSubtopic };
英文:

This is what I've ended up doing:

const MainPostTopic = React.memo(
  function MainPostTopic(props) {
    console.log('Rendering MainPostTopic...');
    return (
      ...
    );
  }
);

const MainPostSubtopic = React.memo(
  function MainPostSubtopic(props) {
    console.log('Rendering MainPostSubtopic...');
    return (
      ...
    );
  }
);

export { MainPostTopic, MainPostSubtopic };

I kept the same name for the functions and the exports.

答案2

得分: 13

尝试

export const MemoMainPostTopic = React.memo(MainPostTopic);

const MainPostTopic = memo(() => {
...
});

export { MainPostTopic };


<details>
<summary>英文:</summary>

Try

export const MemoMainPostTopic = React.memo(MainPostTopic);


or

const MainPostTopic = memo(() => {
...
});

export { MainPostTopic };


</details>



# 答案3
**得分**: 5

```ts
`default` 不是导出的必要命名要求。

const MemoizedMainPostTopic = React.memo(MainPostTopic)

export { MemoizedMainPostTopic }
英文:

Exports that aren't default need to be named.

const MemoizedMainPostTopic = React.memo(MainPostTopic)

export { MemoizedMainPostTopic }

答案4

得分: 1

只需为要导出的对象属性命名即可。

const MainPostTopicComponent = React.memo(MainPostTopic);
const MainPostSubtopicComponent = React.memo(MainPostSubtopic);

export {
  MainPostTopicComponent,
  MainPostSubtopicComponent 
};

希望这可以帮助!

英文:

You just need to give name to the object properties you are exporting.

const MainPostTopicComponent = React.memo(MainPostTopic);
const MainPostSubtopicComponent = React.memo(MainPostSubtopic);

export {
  MainPostTopicComponent,
  MainPostSubtopicComponent 
};

Hope this can help!

答案5

得分: 0

使用默认的 memo:

import { memo } from "react";
const Todos = ({ todos }) => {
  return (
    <>
      <h2>我的待办事项</h2>
      {todos.map((todo, index) => {
        return <p key={index}>{todo}</p>;
      })}
    </>
  );
};

export default memo(Todos);

现在只有在 todos 属性发生变化时组件才会重新渲染。

英文:

Using memo with default:

import { memo } from &quot;react&quot;;
const Todos = ({ todos }) =&gt; {
  return (
    &lt;&gt;
      &lt;h2&gt;My Todos&lt;/h2&gt;
      {todos.map((todo, index) =&gt; {
        return &lt;p key={index}&gt;{todo}&lt;/p&gt;;
      })}
    &lt;/&gt;
  );
};

export default memo(Todos);

Now only if the todos property changed the component will re-render

huangapple
  • 本文由 发表于 2020年1月3日 18:55:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定