我使用Mobx + Reactjs + Firebase +Firebase
我的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 (我选择了单页应用程序,为测试目的设置数据库规则读和写),它创建了几个文件。
但是,无论我如何改变我的代码,我的web应用程序中的数据,都不会更新到我的Firebase数据库中。
发布于 2018-04-24 22:10:34
export var db = firebase.database(app);应:
export var db = app.database();您的syncState应该放在componentDidMount(){...}中。
发布于 2019-03-19 09:52:39
在我的例子中,我是从react工具中升级状态的,不会更新您的防火墙数据库。只有以编程方式使用setState更新状态才会
https://stackoverflow.com/questions/48684018
复制相似问题