问题
I have a component that is at a particular route:
<Route path='/route-path' component={MyComponent} />
In this component all of my firestore listeners are created in componentDidMount like so:
componentDidMount() {
var unsubscribe = db.collection(name)
.where("query", "==", false)
.onSnapshot((querySnapshot) => {
// do something
})
}
Is it necessary to unsubscribe this listener if the route changes? Something like:
componentWillUnmount() {
unsusbscribe() // from componentDidMount
}
or is this handled automatically by route changes, i.e. the websocket closes the connection?
回答1:
The Firestore SDK has no implicit knowledge about ReactJS and/or its lifecycle events.
You'll need to unsubscribe in your own code. It is not done automatically.
来源:https://stackoverflow.com/questions/49868757/firestore-react-is-unsubscribe-necessary-in-componentwillunmount