英文:
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 (
<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?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论