f / delorean

An Agnostic, Complete Flux Architecture Framework
749 stars 40 forks source link

storeListenerMixin can't traverse up to find ancestor's dispatcher #83

Closed bradobro closed 9 years ago

bradobro commented 9 years ago

__findDispatcher() relies on the React view having an attribute, _owner, to help children find the dispatcher without having to pass it all the way down. My React 13.2 views don't have this attribute. Is it a figment of legacy React? Or is there some trick I'm missing?

The mixin still works provided the dispatcher is passed down on the props of all views needing it, so there is a workaround.

Here's the code:

  // `__findDispatcher` is a private function for **React components**.
  function __findDispatcher(view) {
     // Provide a useful error message if no dispatcher is found in the chain
    if (view == null) {
      throw 'No dispatcher found. The DeLoreanJS mixin requires a "dispatcher" property to be passed to a component, or one of it\'s ancestors.';
    }
    /* `view` should be a component instance. If a component don't have
        any dispatcher, it tries to find a dispatcher from the parents. */
    if (!view.props.dispatcher) {
      return __findDispatcher(view._owner);
    }
    return view.props.dispatcher;
  }

Using React 13.2 (13.3 is out but not well described in its changelog)

darcyadams commented 9 years ago

@bradobro sorry for the silence... yes it appears React has removed all ability to reference internal react properties on component classes. For now, any component that is directly passed a the dispatcher as a prop will be able to find it, so a valid workaround is to pass the dispatcher down through your render tree. Of course, this will become tedious quickly. I'm going to attempt a better solution soon, just moving my projects over to 0.13.3 now. Since the dispatcher is a singleton, delorean should be able to register it internally, which would allow the mixin to properly wire things up. Stay tuned, and thanks for reporting the issue.

bradobro commented 9 years ago

Thanks, Darcy!

darcyadams commented 9 years ago

just published v0.9.4 to npm. This version fixes this issue. You no longer need to pass the dispatcher as a prop to any react component.

bradobro commented 9 years ago

Thank!