简体   繁体   English

React.memo() 与 React 功能组件

[英]React.memo() vs React functional component

I use a lot of React functional components in my application.我在我的应用程序中使用了很多 React 功能组件。 Is this a right time to consider replacing my functional components with React's new React.memo() API?现在是考虑用 React 的新React.memo() API 替换我的功能组件的合适时机吗? Will it really improve the performance?它真的会提高性能吗? If i use React.memo() instead of functional components every time React has to check whether my new props and previous props are the same or not, and then needs to update my component.如果我每次都使用React.memo()而不是功能组件,React 必须检查我的新 props 和以前的 props 是否相同,然后需要更新我的组件。 Will it be a burden for React and decrease my performance?会不会给 React 带来负担并降低我的性能?

Any suggestions ?有什么建议 ?

You won't have to replace your functional components, as memo is a higher order component you wrap around your existing component.您不必替换您的功能组件,因为备忘录是您环绕现有组件的高阶组件。 It will improve performance because you will no longer have any unnecessary re-renders of your functional components if their props haven't changed.它将提高性能,因为如果它们的 props 没有改变,您将不再需要对功能组件进行任何不必要的重新渲染。 From the React docs ( https://reactjs.org/docs/react-api.html#reactmemo ):来自 React 文档( https://reactjs.org/docs/react-api.html#reactmemo ):

If your function component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result.如果您的函数组件在给定相同的 props 的情况下呈现相同的结果,您可以将其包装在对 React.memo 的调用中,以便在某些情况下通过记忆结果来提高性能。 This means that React will skip rendering the component, and reuse the last rendered result.这意味着 React 将跳过渲染组件,并重用上次渲染的结果。

You don't have to worry about "replacing" your component, you can just wrap the export function like this您不必担心“替换”您的组件,您可以像这样包装导出功能

export default React.memo(YourComponent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM