Rebase syncState 不更新 Firebase 数据

Rebase syncState does not update Firebase data

我正在使用 Mobx + Reactjs + Firebase + Re-base

我有我的 base.js 喜欢:

import firebase from 'firebase';
import Rebase from 're-base';
import 'firebase/database';

var app = firebase.initializeApp({
    apiKey: "xxxxxx",
    authDomain: "xxxxxx",
    databaseURL: "xxxxx",
    projectId: "xxxxxx",
    storageBucket: "xxxxx",
    messagingSenderId: "xxxxxx"
  });

  export var db = firebase.database(app);
  var base = Rebase.createClass(db);

  export default base;

在我的 TodoList.jsx

@observer
class TodoList extends React.Component {

  constructor(props){
    super(props);

    this.state={
      todos: []
    };

    this.todosRef = base.syncState(`todos`,{
      context: this,
      state:'todos',
      asArray: true,
      then: function(spa){
        console.log("Success!", spa);
      },
      onFailure: function(){
        console.log("Failed!");
      }
    });


    //this.setState({
    //  todos: this.props.store.todos
    //});

    console.log("this.state.todos", this.state.todos);
  }
  ...........

我还做了什么:

我运行 firebase-cli 命令:

firebase init(我选择了单页应用,为了测试目的设置数据库规则读写为真) 它会创建多个文件。

但是无论我如何更改我的代码,我的网络应用程序中的数据都不会更新到我的 Firebase 数据库中。

  export var db = firebase.database(app);

应该是:

export var db = app.database();

您的 syncState 内容应该在 componentDidMount(){...} 中。

在我的例子中,我从反应 chrome 开发工具更新状态,不会 更新你的 firebase 数据库。只有使用 setState 以编程方式更新状态才会