function ScrollView(props) {
const [isScrollingDown, setIsScrollingDown] = useState(false);
useEffect(() => {
setIsScrollingDown(props.row)
}, [props.row])
return `Scrolling down: ${isScrollingDown}`;
}
在这种情况下,我使用useEffect挂钩来更新派生状态,以响应道具的更改。在某些情况下,这是解决派生状态的通用解决方案吗?或者在这种情况下是否有任何问题?
Generally, there are no issues with your implementation, as the
isScrollingDown
is merely reacting to the changes of therow
props value.但是,我确实认为您的场景中不需要使用派生状态,因为逻辑很简单。
消除维护组件状态的需要并消除不必要的重新渲染的任何其他来源将更加直观。