【发布时间】:2020-08-22 23:24:25
【问题描述】:
我应该在每个子组件中使用相同的 useSelector 还是应该通过 props 传递 useSelector?
以下两个示例都有效,我只想知道最佳做法是什么。
通过 props 传递的示例:
const FatherComponent = () => {
const userId = useSelector((state) => state.user.id);
return (
<ChildComponent userId={userId} />
)
}
const ChildComponent = ({userId}) => {
return (
<>{userId}</>
)
}
或重复使用选择器的示例:
const FatherComponent = () => {
const userId = useSelector((state) => state.user.id);
return (
<ChildComponent />
)
}
const ChildComponent = () => {
const userId = useSelector((state) => state.user.id);
return (
<>{userId}</>
)
}
【问题讨论】:
-
谢谢,这正是我所要求的。 ;D
标签: javascript reactjs ecmascript-6 react-redux