reflux / refluxjs

A simple library for uni-directional dataflow application architecture with React extensions inspired by Flux
BSD 3-Clause "New" or "Revised" License
5.36k stars 330 forks source link

Reflux.listenTo's third augument defaultCallback #531

Closed MAYDAY1993 closed 6 years ago

MAYDAY1993 commented 7 years ago

hi,I looked through the docs about Reflux.listenTo's usages in here and find that there are two arguments:one is listenable,the other is callback,but later I find out that there exists a third argument meaning defaultCallback ,I can't find related docs ,only this pr : pr. My problem is :what's the usage of defaultCallback,when to call this function and is there any ducuments to refer to?Thank you 😃

eek commented 7 years ago

If you dig a bit in the code,

listenTo is made of function(listenable,callback,defaultCallback){

Where if defaultCallback exists it goes and fetches the initial state (fetchInitialState) from the listenable, and feeds the data back into defaultCallback

And fetchInitialState has the next explanation:

/**
 * Used in `listenTo`. Fetches initial data from a publisher if it has a `getInitialState` method.
 * @param {Action|Store} listenable The publisher we want to get initial state from
 * @param {Function|String} defaultCallback The method to receive the data
 */

So if the store you are about to listen to, has the getInitialState , you can pass defaultCallback to grab the data retuned from getInitialState and do something with it.

MAYDAY1993 commented 6 years ago

@eek thank you so much 😄