简体   繁体   English

setState()导致道具未定义

[英]setState() causing props to be undefined

I'm brand new to React and am tinkering with some Firebase stuff. 我是React的新手,正在修补一些Firebase的东西。 I'm trying to load in some data from Firebase, filter through some of it, and then set my current state to that data. 我正在尝试从Firebase加载一些数据,对其中的一些进行过滤,然后将当前状态设置为该数据。 For some reason on the code below, calling setState causes the following error: 由于以下代码的某些原因,调用setState会导致以下错误:

Uncaught TypeError: Cannot read property 'database' of undefined 未捕获的TypeError:无法读取未定义的属性“数据库”

import React, {Component} from 'react';

import Activity from './ActivityList'

class ActivityList extends Component {
    constructor(props){
      super(props);

      this.state = {
        activities: []
      }

      let app = this.props.db.database().ref('activities');

      app.on('value', function(snapshot) {

        this.handleData(snapshot.val())

      }.bind(this))
    }

    handleData(values) {

        let category = this.props.category

        let filtered = values.filter(function(item) {
          return item.categories.includes(category)
        })

        console.log(filtered)

        this.setState({activities: filtered})

    }

    render() {
        let activityNodes = this.state.activities.map((activity) => {
          return (
            <Activity activity= {activity} />
            )
      });
        return (
          <div>
            <ul>
             {activityNodes}
            </ul>
          </div>
        );
    }
}

In my code, I am passing in firebase in props.db . 在我的代码中,我正在props.db传递props.db I've also (hopefully) isolated the problem to setState , because my console.log(filtered) call works as intended, meaning on the first "pass", prop/props.db was definitely defined. 我也(希望)将问题隔离到setState ,因为我的console.log(filtered)调用可以按预期工作,这意味着在第一个“ pass”中, prop/props.db已明确定义。 I'm guessing that this has something to do with the non-blocking nature of setState , but I can't for the life of me figure out why this would cause the value of prop to be undefined . 我猜想这与setState的非阻塞性质有关,但是我无法终生弄清楚为什么这会导致prop的值undefined

Edit: Initialization code for db as requested: 编辑:根据要求为db初始化代码:

import * as firebase from 'firebase'

class App extends Component {

  constructor(props) {
    super(props);
    var config = {
      apiKey:,
      authDomain:,
      databaseURL:,
      projectId:,
      storageBucket:,
      messagingSenderId: 
    };
    firebase.initializeApp(config);

  }

This is then passed into a Category component: 然后将其传递到Category组件:

<CategoryList db={firebase}/>

Which then passes it into the above ActivityList component: 然后将其传递到上面的ActivityList组件中:

<ActivityList db={this.props.db}/>

Again, even in the ActivityList component, it seems like it is reading from the database successfully until I call setState . 同样,即使在ActivityList组件中,似乎它也已成功从数据库读取,直到我调用setState为止。

Edited: 编辑:

In chat we found that he was importing something wrong. 在聊天中,我们发现他输入了错误的内容。 That was missing in his question's post. 在他的问题帖子中没有提到这一点。

import Activity from './ActivityList'

Now his map function created ActivityList Components instead of Actitvity Components. 现在,他的地图函数创建了ActivityList组件,而不是Actitvity组件。

But it should be: 但是应该是:

import Activity from './Activity'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM