使用memo()在函数组件内允许吗?

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

Is usage of memo() allowed inside functional component?

问题

I need to create an animated component inside of another function component (there are reasons as to why I can't to do this outside of it).

So far most optimal and cleanest solution I cam up with is following

export function Input({ placeholder }) {
  const AnimatedText = memo(Animated.createAnimatedComponent(Ui.Text))

  return (
    <AnimatedText>
      {placeholder}
    </AnimatedText>
  )
}

I think this works, but have never used memo like this inside a functional component and after looking in the docs, was not able to determine if this is allowed or not, hence this question: Is this ok to do, or should I use useMemo with empty dep array?

英文:

I need to create an animated component inside of another function component (there are reasons as to why I can't to do this outside of it).

So far most optimal and cleanest solution I cam up with is following

export function Input({ placeholder }) {
  const AnimatedText = memo(Animated.createAnimatedComponent(Ui.Text))

  return (
    &lt;AnimatedText&gt;
      {placeholder}
    &lt;/AnimatedText&gt;
  )
}

I think this works, but have never used memo like this inside a functional component and after looking in the docs, was not able to determine if this is allowed or not, hence this question: Is this ok to do, or should I use useMemo with empty dep array?

答案1

得分: 0

Any valid React component, including functions and forwardRef components, is accepted.
参考链接: https://react.dev/reference/react/memo#parameters

You can do so, but react recommends not creating component in a component.


You may not need to use memo
https://react.dev/reference/react/memo#should-you-add-memo-everywhere

英文:

> Any valid React component, including functions and forwardRef components, is accepted.
>
Reference: https://react.dev/reference/react/memo#parameters

You can do so, but react recommends not creating component in a component.


You may not need to use memo

https://react.dev/reference/react/memo#should-you-add-memo-everywhere

huangapple
  • 本文由 发表于 2023年5月13日 18:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242196.html
匿名

发表评论

匿名网友

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

确定