Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应-防止生命周期组件两次呈现相同的数据:WillReceiveProps&;组件安装_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 反应-防止生命周期组件两次呈现相同的数据:WillReceiveProps&;组件安装

Javascript 反应-防止生命周期组件两次呈现相同的数据:WillReceiveProps&;组件安装,javascript,reactjs,redux,Javascript,Reactjs,Redux,针对特定组件的快速反应问题。我使用两种生命周期方法: componentDidMount()-用于在首次呈现组件时检索数据 componentWillReceiveProps(下一步)-用于在某些参数更改时更新数据 这很有效。但是,当我使用组件刷新页面时,两种生命周期方法都会在一种方法足够时执行。页面上的数据仍然正确,但似乎有点低效。如果可能,我如何将这些组合到生命周期中 下面的示例将在刷新页面时调用两次fetchTest()。如果我删除componentDidMount,那么如果用户刷新页

针对特定组件的快速反应问题。我使用两种生命周期方法:

  • componentDidMount()-用于在首次呈现组件时检索数据
  • componentWillReceiveProps(下一步)-用于在某些参数更改时更新数据
这很有效。但是,当我使用组件刷新页面时,两种生命周期方法都会在一种方法足够时执行。页面上的数据仍然正确,但似乎有点低效。如果可能,我如何将这些组合到生命周期中

下面的示例将在刷新页面时调用两次fetchTest()。如果我删除componentDidMount,那么如果用户刷新页面,数据最初不会加载

关于如何让fetchTest()调用一次,无论用户如何访问组件,您有什么想法吗

 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是否不同