logo

该视频仅会员有权观看

立即开通课程「React.js 前端应用开发实践」权限。

¥
199
/ 年

useMemo:在 React 函数式组件里使用记忆值

  • In a React functional component, for computationally intensive operations, consider using useMemo and a hook to perform the calculations, only redoing the calculation when specific data dependencies change.
  • A function makeEmogene is defined within the component, taking a string parameter emogene and returning a piece of UI based on it.
  • makeEmogene defines weather emojis and randomWeather selects a random weather emoji.
  • Another component function, emergeEView, utilizes makeEmogene to display the weather emoji and an emerge string, rerendering only when emerge state changes.
  • useMemo is imported from React and used to ensure that makeEmogene computation only runs when the emerge dependency changes, otherwise, the memoized value is used.
  • Testing confirms that when emerge changes, such as by clicking an emoji button, the displayed weather emoji randomly updates, demonstrating the execution and result of makeEmogene.