简体   繁体   English

React 是否正在远离 React.memo 以支持 useMemo?

[英]Is React moving away from React.memo in favor of useMemo?

Both React.memo and the useMemo hook allow performance optimizations by reducing recalculation and rerendering. React.memouseMemo钩子都可以通过减少重新计算和重新渲染来优化性能。 However, they work definitely in that React.memo is used to wrap a functional component and the useMemo hook can be used on almost any function, even pure calculations.但是,它们确实有效,因为 React.memo 用于包装功能组件,并且useMemo钩子几乎可以在任何 function 上使用,甚至是纯计算。 More importantly, useMemo is typically called from a parent to a child component, while React.memo is typically called on the declaration of the child itself.更重要的是, useMemo通常是从父组件调用到子组件,而React.memo通常是在子组件本身的声明上调用。

While both have different advantages, one advantage of React.memo is that it doesn't have to be called from each parent to child relationship.虽然两者都有不同的优点,但React.memo的一个优点是不必从每个父子关系调用它。 However, with the release of hooks, it seems apparent that React development wants to move towards functional components that use hooks to deal with state, side-effect events, and other effects.然而,随着 hooks 的发布,React 开发显然希望转向使用 hooks 来处理 state、副作用事件和其他影响的功能组件。 While I don't think that the React development team would have the courage or disregard of their user base to remove React.memo any time in the next 2 years, it makes me wonder if they want end users to stop using React.memo for stylistic reasons.虽然我认为 React 开发团队不会有勇气或无视他们的用户群在未来 2 年的任何时候删除React.memo ,但这让我想知道他们是否希望最终用户停止使用 React.memo风格原因。 Just as they have kind of passive aggressively moved away from class components in favor of functional components with hooks.就像他们积极地从 class 组件转移到带钩子的功能组件一样。

When using functional components with hooks, is the react framework moving away from React.memo ?当使用带有钩子的功能组件时,react 框架是否正在远离React.memo Would it be better to get used to using the hook version if one wants to keep up with React best practices in the future?如果将来想跟上 React 的最佳实践,习惯使用 hook 版本会更好吗?

The short answer is no.最简洁的答案是不。

Both are used to optimise performance in regard to reducing unnecessary re-rendering, but React.memo and useMemo are used for two different scenarios...两者都用于优化性能以减少不必要的重新渲染,但React.memouseMemo用于两种不同的场景......

React.memo is a HOOC and informs react to perform a shallow comparison of the passed in props to determine whether to re-render. React.memo是一个 HOOC,它通知 react 对传入的 props 进行浅层比较,以确定是否重新渲染。

https://reactjs.org/docs/react-api.html#reactmemo https://reactjs.org/docs/react-api.html#reactmemo

Example:例子:

export const Component = React.memo(({name}) => `Hello ${name}`)

Here, react will to a shallow comparison, and will only re-render if name has changed.在这里,react will 进行浅比较,并且仅在名称更改时才会重新渲染。

useMemo is a hook and is used to memoize a value. useMemo是一个钩子,用于记忆一个值。 React will only only re-evaluate the value if the dependencies (second arg of useMemo ) change.只有当依赖项( useMemo的第二个参数)发生变化时,React 才会重新评估该值。 There are usage rules with hooks that should be followed.应该遵循带有钩子的使用规则

https://reactjs.org/docs/hooks-reference.html#usememo https://reactjs.org/docs/hooks-reference.html#usememo

Example:例子:

export const MyComponent = ({firstName, lastName, age}) => {

    const fullName = useMemo(() => `${firstName} ${lastName}`, [firstName, lastName]);

    return <Profile fullName={fullName} />

}

You could hack useMemo to do something like React.memo, but it is not intended to be used that way.你可以破解 useMemo 来做类似 React.memo 的事情,但它不打算以这种方式使用。

useCallback There is also the useCallback hook that is often used with React.memo . useCallback还有一个经常与React.memo一起使用的useCallback钩子。

If your parent component passes a callback to a child that is wrapped in React.memo , it's a good idea to create the callback using useCallback otherwise the child will re-render anyway due to the callback being re-created each time the parent re-renders.如果您的父组件将回调传递给包含在React.memo中的子组件,则最好使用useCallback创建回调,否则子组件将重新渲染,因为每次父组件都重新创建回调呈现。

useCallback also takes a dependency array like useMemo so that it can be re-created if a dependency changes. useCallback也接受一个像useMemo这样的依赖数组,以便在依赖改变时可以重新创建它。

https://reactjs.org/docs/hooks-reference.html#usecallback https://reactjs.org/docs/hooks-reference.html#usecallback

Example:例子:

export const MyComponent = ({firstName, lastName, age}) => {

    const handleClick = useCallback((e) => {
        e.preventDefault();
        // doSomething
    }, []);

    return <Profile onClick={handleClick} />

}

const Profile = React.memo((onClick) => (
    <button onClick={onClick)>Click me!</button>
));

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

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