Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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 获取帮助程序方法以在初始状态更新时运行_Javascript_Reactjs_Redux - Fatal编程技术网

Javascript 获取帮助程序方法以在初始状态更新时运行

Javascript 获取帮助程序方法以在初始状态更新时运行,javascript,reactjs,redux,Javascript,Reactjs,Redux,我的目标是在this.props.usersave初始更新为state时运行automagination函数。在我的程序中,它开始时是一个空数组,初始化时数组中存储了100个对象。问题是,在存储对象之前,自动贴图正在运行,因此while循环没有运行。我已经用setTimeout解决了这个问题,但我并不认为这是一个长期的解决方案。有什么想法吗 下面的代码嵌套在基于类的组件中 autoPagination = async token => { while (this.props.u

我的目标是在this.props.usersave初始更新为state时运行automagination函数。在我的程序中,它开始时是一个空数组,初始化时数组中存储了100个对象。问题是,在存储对象之前,自动贴图正在运行,因此while循环没有运行。我已经用setTimeout解决了这个问题,但我并不认为这是一个长期的解决方案。有什么想法吗

下面的代码嵌套在基于类的组件中

  autoPagination = async token => {
    while (this.props.userSaves.length > 0) {
      const { userSaves } = this.props
      const lastPage = userSaves[userSaves.length-1].data.name

      const userSavesObject = await axios.get (`https://oauth.reddit.com/user/${this.props.username}/saved/.json?limit=100&after=${lastPage}`, {
      headers: { 'Authorization': `bearer ${token}` }
    })
      const currentPageSaves = userSavesObject.data.data.children
      this.props.storeUserHistory(currentPageSaves)
      this.props.appendUserHistory(currentPageSaves)
    }
  }
完整组件(根据要求):

从“axios”导入axios;
从“React”导入React;
从'react redux'导入{connect};
从“../actions/index.js”导入{storeUserHistory,appendUserHistory,storeInitialData}
类ListResact.Component{
组件安装(道具){
const params=新的URLSearchParams(this.props.location.hash);
const token=params.get(“#访问_token”)
this.props.storeInitialData(令牌)
设置超时(()=>{
这个。自动激发(代币);
}, 3000)
}
自动映射=异步令牌=>{
而(this.props.userSaves.length>0){
const{userSaves}=this.props
const lastPage=userSaves[userSaves.length-1].data.name
const userSavesObject=wait axios.get(`https://oauth.reddit.com/user/${this.props.username}/saved/.json?limit=100&after=${lastPage}`{
标头:{'Authorization':'bearer${token}}}
})
const currentPageSaves=userSavesObject.data.data.children
this.props.storeUserHistory(currentPageSaves)
this.props.appendUserHistory(currentPageSaves)
}
}
renderPostTitles=()=>{
返回此.props.totalSaves.map((已保存)=>{
返回(
{已保存的.data.title}
)
})
}
渲染(){
返回{this.renderPostTitles()}
}
}
常量mapStateToProps=状态=>{
console.log(状态)
返回{
用户名:state.username,
userSaves:state.userHistory,
totalSaves:state.totalUserHistory
}
}
导出默认连接(MapStateTops,{storeUserHistory,appendUserHistory,storeInitialData})(ListSaved);

获取一个变量并在开始时将其设置为真。。在props中获取数据时运行该函数,并将变量设为false,使其不再运行

constructor (props)
{
   super(props)
   this.myvar = true
}

componentWillRecieveProps(nextProps)
{
    if(this.myvar)
    {
         if(check if get your data)
           {
               // run your function 
              this.myvar= false
          }
     }
}

修正组件。每次组件更新时,函数都会运行。组件在安装后第一次更新

从“axios”导入axios;
从“React”导入React;
从'react redux'导入{connect};
从“../actions/index.js”导入{storeUserHistory,appendUserHistory,storeInitialData}
类ListResact.Component{
组件安装(道具){
const params=新的URLSearchParams(this.props.location.hash);
const token=params.get(“#访问_token”)
this.props.storeInitialData(令牌)
}
组件更新(道具){
这个。自动激发(代币);
}
自动映射=异步令牌=>{
而(this.props.userSaves.length>0){
const{userSaves}=this.props
const lastPage=userSaves[userSaves.length-1].data.name
const userSavesObject=wait axios.get(`https://oauth.reddit.com/user/${this.props.username}/saved/.json?limit=100&after=${lastPage}`{
标头:{'Authorization':'bearer${token}}}
})
const currentPageSaves=userSavesObject.data.data.children
this.props.storeUserHistory(currentPageSaves)
this.props.appendUserHistory(currentPageSaves)
}
}
renderPostTitles=()=>{
返回此.props.totalSaves.map((已保存)=>{
返回(
{已保存的.data.title}
)
})
}
渲染(){
返回{this.renderPostTitles()}
}
}
常量mapStateToProps=状态=>{
console.log(状态)
返回{
用户名:state.username,
userSaves:state.userHistory,
totalSaves:state.totalUserHistory
}
}

导出默认连接(MapStateTops,{storeUserHistory,appendUserHistory,storeInitialData})(ListSaved)此方法仅在创建组件时调用。尝试在
shouldComponentUpdate()
中调用它,如果可能,在shouldComponentUpdate中调用它会创建一个无限循环。并给出错误ListSaved.shouldComponentUpdate():返回未定义的值,而不是布尔值。确保在上返回true或false。-在每个循环上,它不应该创建一个无限的LNIFENT循环,但不能在没有看到组件的情况下创建更多的循环。始终在
shouldComponentUpdate()
函数中返回bool,因此只需添加
return true
就可以了。尝试了这一点,仍然在无限循环中运行。没有发布组件的其余部分,因为我认为它与Q无关,可能只是让人困惑。组件中只有存储访问令牌的componentDidMount和显示标题的映射函数。ComponentWillReceiveProps自React 16.3Ok以来被标记为不安全且不推荐使用。我们可以在componentDidUpdate中使用同样的方法。我以前也尝试过这种方法,我也尝试过订阅redux应用商店,这样它每次都会更新。两者都不起作用。任何时候,以这种方式运行automagination函数时,它都会进入一个无限循环,导致chrome崩溃。仅供参考,我复制/粘贴了要测试的代码,它再次进入无限循环。这就是为什么在OP中我只想让它运行一次。“const currentPageSaves=userSavesObject.data.data.children this.props.storeUserHistory(currentPageSaves)this.props.appendUserHistory(currentPageSaves)``这是导致无限循环的部分。确保不更改道具,道具应以只读方式访问
constructor (props)
{
   super(props)
   this.myvar = true
}

componentWillRecieveProps(nextProps)
{
    if(this.myvar)
    {
         if(check if get your data)
           {
               // run your function 
              this.myvar= false
          }
     }
}