在使用ES6语法时,将firebase数据加载到react项目中的“最佳实践”是什么?在es6中不支持混合,因此reactfire似乎不是一个选项
我采用了这种方法,但它真的不是很优雅,我想听听如何做到这一点的一些可能的解决方案。在构造函数方法中将this.items声明为this.items = []
componentWillMount() {
this.firebaseRef = new Firebase(SETTINGS.FIREBASEURL + 'todos');
let self = this;
this.firebaseRef.on('child_added', function(snapshot) {
self.items.push(snapshot.val());
self.setState({data: self.items});
});
}
componentDidMount() {
let self = this;
let intVal = setInterval(function() {
if (self.items.length > 0){
self.setState({data: self.items});
self.forceUpdate();
clearInterval(intVal);
}
}, 200);
}
componentWillUnmount() {
this.firebaseRef.off();
}
render() {
console.log(this.state.data);
return <p>Hello, Kitty</p>;
}当我将其加载到浏览器中时,数据被更新了两次。如何避免在显示state.data时使用forceUpdate()
发布于 2016-01-12 06:29:17
请从React站点查看此内容:https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#mixins。
现在看来,在这种情况下,您必须使用较旧的语法!
对于更新问题,请查看生命周期的componentShouldUpdate方法。https://facebook.github.io/react/docs/component-specs.html#updating-shouldcomponentupdate
发布于 2016-11-08 01:51:11
我刚刚开始研究这个问题,我遇到的所有其他答案都指向这个替代方案:
https://github.com/tylermcginnis/re-base
来自作者:
ReactFire的问题是因为它使用了Mixin,它与ES6类不兼容。在与雅各布·特纳交谈后,我们想要创建一种方法,允许ReactFire与ES6类的单向绑定,以及一些更多的功能,比如双向数据绑定和监听Firebase端点,而不需要实际绑定状态属性到它们。如此一来,就建立了re-base。
https://stackoverflow.com/questions/34732079
复制相似问题