Reactgo: Passing arguments into need

Created on 23 Mar 2016  路  2Comments  路  Source: reactGo/reactGo

Hi,

Great work on this, enjoying tinkering with it.

I have a question about using need/ fetchComponentDataBeforeRender().

In a container, what's the best way to pass in an argument with an action specified in need?

i.e. something like this won't work:

  static need = [
    fetchPage(id)
  ]

In my project I have an undetermined number of content items which I want to fetch and add to state only as and when they're requested, rather than all in one go.

The boilerplate doesn't have this issue as it fetches all topics in need.

Any suggestions about the best way to do it gratefully received! There's probably a really obvious solution I'm not seeing.

Cheers
Richard

Most helpful comment

Thanks very much for the pointers @Epochi !

That's helped me zero in on the issue - as it turns out it was all actually working fine and it was just down to me making a really basic error. The params object was being passed in - and I was forwarding that object to the api route, which was just expecting a string. No wonder nothing ever came back.

Thanks again for the explanation, that's really clarified how it works for me.

Cheers
Richard

All 2 comments

Hey @ReallySmall ,

I think static need = [ fetchPage ]just points the the action inside actions/pages, and you have to specify the arguments to accept there. I'm 90% sure about that.

Unmodified fetchComponentDataBeforeRender uses dispatch(need(params), so if you have any route params in react-router i.e.<Route path='posts/:id'> it would pass the wildcard id to your need.

you can also change around fetchComponentDataBeforeRender arguments, for example

app/server.js

fetchComponentDataBeforeRender(store, renderProps.components, id, anyCustomData)

and then inside the api itself

export function fetchComponentDataBeforeRender(store, components, id, data) {
  const needs = components.reduce( (prev, current) => {
    return (current.need || [])
      .concat((current.WrappedComponent ? current.WrappedComponent.need : []) || [])
      .concat(prev);
    }, []);
    const promises = needs.map(need => need(store,id,data);
    return Promise.all(promises);
}

inside component

  static need = (store, id) => {
    return store.dispatch(loadPost(id));
  };  

Thanks very much for the pointers @Epochi !

That's helped me zero in on the issue - as it turns out it was all actually working fine and it was just down to me making a really basic error. The params object was being passed in - and I was forwarding that object to the api route, which was just expecting a string. No wonder nothing ever came back.

Thanks again for the explanation, that's really clarified how it works for me.

Cheers
Richard

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jrodl3r picture jrodl3r  路  6Comments

zacharybrady picture zacharybrady  路  4Comments

choonkending picture choonkending  路  4Comments

Cleanshooter picture Cleanshooter  路  7Comments

choonkending picture choonkending  路  8Comments