首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重基syncState不更新Firebase数据

重基syncState不更新Firebase数据
EN

Stack Overflow用户
提问于 2018-02-08 11:06:34
回答 2查看 950关注 0票数 0

我使用Mobx + Reactjs + Firebase +Firebase

我的base.js就像:

代码语言:javascript
复制
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上

代码语言:javascript
复制
@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 (我选择了单页应用程序,为测试目的设置数据库规则读和写),它创建了几个文件。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-04-24 22:10:34

代码语言:javascript
复制
  export var db = firebase.database(app);

应:

代码语言:javascript
复制
export var db = app.database();

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

票数 1
EN

Stack Overflow用户

发布于 2019-03-19 09:52:39

在我的例子中,我是从react工具中升级状态的,不会更新您的防火墙数据库。只有以编程方式使用setState更新状态才会

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48684018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档