Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement initial data fetch #55

Closed
bardt opened this issue May 16, 2016 · 5 comments
Closed

How to implement initial data fetch #55

bardt opened this issue May 16, 2016 · 5 comments
Labels

Comments

@bardt
Copy link

bardt commented May 16, 2016

If I correctly understand the intention of this library, it is supposed to move decision making as close to reducers as possible. Classic example of React+redux flow is to fetch data needed to render a React component from componentDidMount lifecycle hook in the component itself:

class DataViewer extends React.Component {
  componentDidMount() {
    this.props.dispatch(fetchDataIfNeeded());
  }

  render() {
    return (
      <div>
        { this.props.data ? 
          <DataList data={ this.props.data }/> : 
          <Preloader /> 
        }
      </div>
    );
  }
}

If we try to move this logic inside reducer, we can add fetchDataIfNeeded to Effect returned after an action leading to displaying this component occurred, like in this example.

Additionally, we have to handle the initial state leading to display DataViewer without any action made yet. Example situation:

  1. The initial state is restored from URL.
  2. The initial state is passed to createStore.
  3. We don't want to handle data fetching manually, but instead, we assume reducer to check if we have enough data to show DataViewer and return a loop with fetchDataIfNeeded if it is the case.

The question is how to handle this init step in reducer. Using @@INIT action is considered an anti-pattern. @@ReduxLoop/INIT happens later if we already returned initial effect.

So, what is the suggested way to decide the need of initial effects?

@bdwain
Copy link
Member

bdwain commented May 16, 2016

i think the code you have (using componentdidmount) makes sense. the component needs the data, so it should be the one that asks for it.

@bardt
Copy link
Author

bardt commented May 17, 2016

@bdwain, as we are trying to implement more Elm architecture principles around Redux (and redux-loop makes a great job), the view is supposed to be as dumb as possible. If we use some dumber alternative to React without such lifecycle hooks, wich would be the place for such data fetching logic right after store init?

@bdwain
Copy link
Member

bdwain commented May 17, 2016

If you want to do it right after store init you could just dispatch the action manually right after you create the store.

const store = createStore(...)
store.dispatch(createFetchInitialDataAction())

@lukewestby
Copy link
Contributor

lukewestby commented May 23, 2016

@bdwain's suggestion is typically how we handle it at Raise. This let's make use of dispatch being modified to return a promise as well, in case you want to wait for your initial data fetch to complete before rendering or doing anything else.

@lukewestby
Copy link
Contributor

lukewestby commented Oct 20, 2016

I'd like to push this off as a task for version 4.0 or 3.1 or whatever it ends up being. I'm getting ready to release 3.0 shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants