reactjs - What is the reasoning behind the following Redux paradigm? -


i have been seeing following snippet in couple of react/redux tutorials:

componentwillunmount() {     this.unsubscribe(); }  componentdidmount() {     this.unsubscribe = this.context.store.subscribe(() => {         this.forceupdate();     }); } 

what purpose of code? when component unmounted, doesn't code call forceupdate whenever store changes? why want that?

i think you're misreading snippet bit.

store.subscribe() takes callback run every time action dispatched store. returns function remove specific callback store's list of listeners.

in example, component have this.forceupdate() called each time action dispatched. when component unmounted, calls this.unsubscribe(), remove listener store.

however, "raw" use of store api in react component anti-pattern. react-redux library abstracts out process of interacting store, , should use connect react components redux. similar abstractions exist angular, ember, vue, , more. there's no reason write own store subscription code.

as side note, i'd appreciate if leave comment showing saw snippet, because i'd go encourage them not that.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -