brocoders / redux-async-connect

It allows you to request async data, store them in redux state and connect them to your react component.
644 stars 103 forks source link

If route has params, how these params are transferred to redux-async-connect? #53

Open whatifif opened 8 years ago

whatifif commented 8 years ago

For example, if the route is /widget/:id, how the id value is transferred to redux-async-connect?

andresgutgon commented 8 years ago

If you want to fetch data for your component based on URL params this is how I'm doing it:

@asyncConnect([{
  promise: (options) => {
    const {
      store: { dispatch },
      params: { id },
    } = options;

    return dispatch(loadYourDataWidget(id));
  },
}])

As you can see you have params in options object. You should have the :id passed by URL

blainegarrett commented 8 years ago

@andresgutgon Thank you for posting this. I just found myself needing to figure this out too. I'm newish to ES6, so this syntax threw me off a bit.

For anyone else struggling with this, here is my syntax that currently working.

// Ensure all articles are loaded regardless of route params. Equivalent to: options.store.dispatch
@asyncConnect([{
  promise: ({store: {dispatch}}) => {
    const promises = [];
    promises.push(dispatch(getArticles())); 
    ...
    return Promise.all(promises);
  }
}])

As opposed to:

// Include the route params too - Equivalent to: options.params,  options.store.dispatch
@asyncConnect([{
  promise: ({params, store: {dispatch}}) => {
    const promises = [];
    promises.push(dispatch(getArticleBySlug(params.slug)));
    ...
    return Promise.all(promises)
  }

Here are the full set of properties on that options object passed in.

brendanscarano commented 8 years ago

Thank you @andresgutgon - this was extremely helpful!

andresgutgon commented 8 years ago

You're welcome :)

georgegillams commented 6 years ago

@blainegarrett Thanks for elaborating with your example. My existing syntax looked more like yours, so this massively helped me!