martyjs / marty

A Javascript library for state management in React applications
http://martyjs.org
MIT License
1.09k stars 77 forks source link

Component halts rendering when loading remote state from Container #307

Closed anthonator closed 9 years ago

anthonator commented 9 years ago

When mounting a component that requires data be fetched remotely using a container/store/query the component will stop rendering until the data is fetched. This effectively prevents me from having the ability to show load states. I just have empty areas on the page where components should be.

Component

render() {
    let component;

    if (this.props.loading) {
      component = this._renderLoading();
    } else {
      component = this._renderConversations();
    }

    return (
      <div className='conversations panel left'>
        { component }
      </div>
    );
  }

Container

return Marty.createContainer(Component, {
    listenTo: ConversationStore,

    fetch: {
      conversations() {
        return ConversationStore.for(this).all();
      },

      loading() {
        return ConversationStore.for(this).isLoading(ConversationConstants.FETCH_ALL_CONVERSATIONS);
      }
    }
  });
anthonator commented 9 years ago

If I put a console.log in the render() method nothing shows up in the console until the remote fetch has completed.

anthonator commented 9 years ago

Just discovered pending in containers. Fixes my problem.

taion commented 9 years ago

FWIW, looking at this a bit, it does seem like it might be tricky to deal with pending when there are potentially multiple data fetches outstanding. Maybe best to just use multiple containers in that case.

jhollingworth commented 9 years ago

FYI We pass any finished fetches into the pending handler if you want to define default values

taion commented 9 years ago

My bad. Could be worth updating the docs to reflect this, then, unless they already say this and I just missed it.

jhollingworth commented 9 years ago

That feature is only 4 days old (#300), It's in the v0.10 docs but will back port to v0.9 over the weekend

taion commented 9 years ago

Ah! That makes sense then. Thanks!