我有一个 React Redux 应用程序,它将在多个 Firebase 端点上有监听器。这些监听器将在应用程序的不同位置安装和卸载,我打算根据数据库更改动态生成这些监听器。
问题
- 我可以查看
firebase
对象上的事件监听器数量吗? - 在下面的代码中,
var firebaseRef
是对实际firebase
对象的引用,正确吗?因此,所有监听器实际上都在firebase
对象上,并且如果组件在未调用.off()
的情况下卸载,则不会被销毁? - 我应该担心大约 10 名活跃听众吗?
- 有谁知道在数据库引用上不使用
.off()
是否会破坏代码?或者说它会显着改变性能?
The Firebase Documentation has it mounted in ComponentWillMount
componentWillMount: function() {
var firebaseRef = firebase.database().ref('todoApp/items');
this.bindAsArray(firebaseRef.limitToLast(25), 'items');
}
并在ComponentWillUnmount
处删除
componentWillUnmount: function() {
this.firebaseRef.off();
}
谢谢!
最佳答案
- Can I view the number of active listeners on the firebase object?
没有 API 可以返回客户端中事件监听器的数量。但您可以轻松地自己保留一个计数器。
- In the code below,
var firebaseRef
is a reference to the actual firebase object correct? Therefore, all listeners are really on the firebase object and will not be destroyed if the component unmounts without calling.off()
?
引用
实际上只是有关数据库中某个位置的信息。保留它们非常便宜。
- Should I worry about having ~10 active listeners?
听众数量并不是什么大问题。您应该考虑的是您正在读取的数据量。一般来说,我建议仅同步您(主动或频繁)向用户显示的数据。
- Does anyone know if not using
.off()
on database references will break code? Or that it will significantly alter performance?
调用或不调用 .off()
都不会破坏代码。但是,一旦您使用 on()
将监听器附加到某个位置,该位置的数据就会同步,直到您调用 off()
。在您的应用中拥有大量挥之不去的监听器可能会导致读取用户看不到的大量数据。
关于javascript - 在 React 应用程序中查看/管理 Firebase 监听器的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45379377/