刷新后无法连接到阵列状态

时间:2018-10-09 05:17:05

标签: javascript reactjs firebase react-native

所以这是一个棘手的问题需要解释。基本上,我要做的是查询firebase以获取一些信息,然后在列表视图中显示该信息。这是第一次工作,但是当我刷新组件时,我的列表视图仅显示我获取的最后一个值。

刷新没有问题,所有状态都显示正确的信息,控制台日志指示变量仍然相同。但是我似乎无法再追加到我的食谱列表状态了。

handleUpdateIndex (selected) {
firebase.database().ref('recipes').orderByChild('custard').on('child_added', (snapshot) => {

    var variable = snapshot.val();
    var first = "variable.";
    var last = " === 0";
    var joined = this.state.unavailable.join(' === 0 && variable.');
    var before = first.concat(joined);
    var final = before.concat(last);

    if (eval(final)){
        console.log(variable)
        this.setState({
        recipelist: this.state.recipelist.concat(
          variable
        )
      })
    }
  });

this.setState({selectedIndex : selected})
}

2 个答案:

答案 0 :(得分:0)

尝试一下

   LogicException  : Unable to prepare route [api/user] for serialization. Uses Closure.

  at /var/www/html/comefo/vendor/laravel/framework/src/Illuminate/Routing/Route.php:880
    876|      */
    877|     public function prepareForSerialization()
    878|     {
    879|         if ($this->action['uses'] instanceof Closure) {
  > 880|             throw new LogicException("Unable to prepare route [{$this->uri}] for serialization. Uses Closure.");
    881|         }
    882| 
    883|         $this->compileRoute();
    884| 

  Exception trace:

  1   Illuminate\Routing\Route::prepareForSerialization()
      /var/www/html/comefo/vendor/laravel/framework/src/Illuminate/Foundation/Console/RouteCacheCommand.php:62

  2   Illuminate\Foundation\Console\RouteCacheCommand::handle()
      /var/www/html/comefo/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:29

  Please use the argument -v to see more details.

还请确保您在类似的构造函数中绑定handleUpdateIndex

  this.setState(prevState => ({
    recipelist: [...prevState.recipelist, variable]
  }));

答案 1 :(得分:0)

好的,几个小时后我已经修复了。

var samples = [];
firebase.database().ref('recipes').orderByChild('custard').on('child_added', (snapshot) => {

    var variable = snapshot.val();
    var first = "variable.";
    var last = " === 0";
    var joined = this.state.unavailable.join(' === 0 && variable.');
    var before = first.concat(joined);
    var final = before.concat(last);
    var string = variable.toString();
    if (eval(final)){
        samples.push(variable);
    }
  });
this.setState({
    recipelist: samples
})