arunoda / react-komposer

Feed data into React components by composing containers.
MIT License
732 stars 70 forks source link

Dispatch action inside reactiveMapper #141

Closed Ethaan closed 7 years ago

Ethaan commented 7 years ago

Is this ok?

Setup

currently im using the getTrackerLoader function from the README.

and my mapper function looks like this.

function mapMongoToProps(props, onData) {
  const authorsHandler= Meteor.subscribe("getAllAuthors");
  if (authorsHandler.ready()) {
    store.dispatch(setInitialAuthorsLists(Authors.find().fetch())); // this is causing errors
    onData(null, {})
  }
}

im also using setDefaults like.

const customCompose = setDefaults({
    pure: true,
    loadingHandler: Loader,
});

and this how the Containers is composed.

DragContainer = merge(
  customCompose(getTrackerLoader(mapMongoToProps)),
  customCompose(composeWithRedux(mapStateToProps))
)(DragContainer);

export default DragContainer;

My composeWithRedux looks like.

export default function(mapper) {
  return (props, onData) => {
    const state = store.getState();
    onData(null, mapper(state));
    return store.subscribe(() => {
      const state = store.getState();
      onData(null, mapper(state));
    });
  }

and my reduxMapper looks like.

function mapStateToProps(state) {
  return {
    heroes: state.authors.heroes,
    villans: state.authors.villans
  }
}

The PROBLEM

Dispatching store.dispatch(setInitialAuthorsLists(Authors.find().fetch())); inside the trackerMapper is causing the Loading... screen to never ends.

Debug

using React Logger, shows something like this

screen shot 2016-12-11 at 1 42 05 pm

The Question.

is okay to dispatch actions inside the reactiveMapper for the Tracker?

arunoda commented 7 years ago

It is bad since it'll add a recursive changes since you are using composeWithRedux. I think this integration between Meteor and Redux is not good.

You need to integrate Meteor and Redux outside of the composers.

Ethaan commented 7 years ago

@arunoda got it, just testing and playing around.

Thanks!