Javascript 反应-防止生命周期组件两次呈现相同的数据:WillReceiveProps&;组件安装
针对特定组件的快速反应问题。我使用两种生命周期方法:Javascript 反应-防止生命周期组件两次呈现相同的数据:WillReceiveProps&;组件安装,javascript,reactjs,redux,Javascript,Reactjs,Redux,针对特定组件的快速反应问题。我使用两种生命周期方法: componentDidMount()-用于在首次呈现组件时检索数据 componentWillReceiveProps(下一步)-用于在某些参数更改时更新数据 这很有效。但是,当我使用组件刷新页面时,两种生命周期方法都会在一种方法足够时执行。页面上的数据仍然正确,但似乎有点低效。如果可能,我如何将这些组合到生命周期中 下面的示例将在刷新页面时调用两次fetchTest()。如果我删除componentDidMount,那么如果用户刷新页
- componentDidMount()-用于在首次呈现组件时检索数据
- componentWillReceiveProps(下一步)-用于在某些参数更改时更新数据
componentDidMount() {
fetchTest(this.props.params.id);
// for initial component render
}
componentWillReceiveProps(nextProps) {
fetchTest(nextProps.params.id);
// for when (params.id) is changed. data within component is updated
}
你可能想这样做
componentWillReceiveProps(nextProps) {
if (nextProps.params.id !== this.props.params.id) {
fetchTest(nextProps.params.id);
}
}
你可能想这样做
componentWillReceiveProps(nextProps) {
if (nextProps.params.id !== this.props.params.id) {
fetchTest(nextProps.params.id);
}
}
你的问题措辞奇怪。如果您想保证只调用一次
fetchTest
,那么就不要包含组件willreceiveprops
。。但是,如果您想在新建参数时保持调用fetchTest
的行为,请将this.props.params
与next.props.params
进行比较,以查看id是否不同。您的问题措辞奇怪。如果您想保证只调用一次fetchTest
,那么就不要包含组件willreceiveprops
。。但是,如果要在新建params
时保持调用fetchTest
的行为,请将this.props.params
与next.props.params
进行比较,查看id是否不同