0

我想显示来自我的 firebase 数据库的数据以响应引导表。但是,如果刷新后,我无法显示实时数据。

这是我的反应组件代码

import React, { Component } from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
import PageHeader from 'react-bootstrap/lib/PageHeader';
import database from './database';

const ref = database.ref('users');
  ref.on('value', gotData, errData); 

const products = [];

function gotData(data) {

  const userdata = data.val();
  const keys = Object.keys(userdata);
  for (let i = 0; i < keys.length; i++) {
    const k = keys[i];
    products.push({
      name: userdata[k].nama, address: userdata[k].address, email: userdata[k].email
    });
  }
}

function errData(err) {
  console.log(err);
}

class Member extends Component {
    constructor(props) {
    super(props);
    this.state = {
      text:''
    };
  }


  handleClick = (rowKey) => {
    alert(this.refs.table.getPageByRowKey(rowKey));
  }

    render(){

    return (
      <div>
          <div className="col-lg-12">
          <PageHeader>Members</PageHeader>
       </div>
        <BootstrapTable  
              ref='table'
            data={ products }
            pagination={ true }
            search={ true }>
          <TableHeaderColumn dataField='name' isKey={true} dataSort={true}>Name</TableHeaderColumn>
            <TableHeaderColumn dataField='address' dataSort={true}>Address</TableHeaderColumn>
            <TableHeaderColumn dataField='email'>Email</TableHeaderColumn>
          </BootstrapTable>
      </div>
    );
    }
}

export default Member;

这是我的初始化数据库代码

import firebase from 'firebase';

const config = {
  apiKey: '......',
  authDomain: '......',
  databaseURL: '....',
  projectId: '....',
  storageBucket: '....',
  messagingSenderId: '....'
};

firebase.initializeApp(config);
const database = firebase.database();


export default database;

在这方面帮助我,谢谢。

4

1 回答 1

0

re-render当数据发生变化时,您必须告诉组件。我认为与相关的功能不firebase应该存在于组件之外。相反,它们应该存在于组件中。您应该执行以下操作。希望能帮助到你

import React, { Component } from 'react';
import { BootstrapTable, TableHeaderColumn } from 'react-bootstrap-table';
import PageHeader from 'react-bootstrap/lib/PageHeader';
import database from './database';


class Member extends Component {
    constructor(props) {
    super(props);
    this.state = {
      text:'',
      products: []
    };

   this.userRef = database.ref('users');
  }

  componentDidMount() {
    this.userRef.on('value', this.gotData, this.errData); 
  }


  gotData = (data) => {
    let newProducts = []
    const userdata = data.val();
    const keys = Object.keys(userdata);
    for (let i = 0; i < keys.length; i++) {
      const k = keys[i];
      newProducts.push({
        name: userdata[k].nama, address: userdata[k].address, email: userdata[k].email
      });
    }
    this.setState({products: newProducts});
  }

  errData = (err) => {
   console.log(err);
 }

  handleClick = (rowKey) => {
    alert(this.refs.table.getPageByRowKey(rowKey));
  }


render(){

  return (
  <div>
      <div className="col-lg-12">
      <PageHeader>Members</PageHeader>
   </div>
    <BootstrapTable  
        ref='table'
        data={ this.state.products }
        pagination={ true }
        search={ true }>
      <TableHeaderColumn dataField='name' isKey={true} dataSort={true}>Name</TableHeaderColumn>
        <TableHeaderColumn dataField='address' dataSort={true}>Address</TableHeaderColumn>
        <TableHeaderColumn dataField='email'>Email</TableHeaderColumn>
      </BootstrapTable>
    </div>
  );
 }
}

export default Member;

所有subscription相关代码都会进入componentDidMount,您必须unsubscribe在组件卸载时进行。

于 2017-04-11T03:42:45.170 回答