guidsen / react-simple-universal

Simple universal approach for React Apps
MIT License
87 stars 6 forks source link

Implement data fetching #22

Open guidsen opened 8 years ago

guidsen commented 8 years ago

What will be the best way to pre fetch data? In #1 we've talked a bit about it as well. What options do we have and is most straightforward?

Extending the react-router Route component with a property that handles data fetching? How would we handle data fetching from server and from client, because from the client we have to make AJAX requests where in the server we can return some kind of db promise with results.

cc @d6u @jackfranklin

langpavel commented 8 years ago

Nice isomorphic fetch is implemented in react-starter-kit. They use package.json in subdir to separate client and server code like this:

{
  "private": true,
  "name": "fetch",
  "main": "./fetch.server.js",
  "browser": "./fetch.client.js"
}

See react-starter-kit /src/core/fetch

guidsen commented 8 years ago

@langpavel If I'm right that means that you always make requests to an URL, right?

langpavel commented 8 years ago

Yes. It's one simplest and common pattern. This allows you to change backend in the future. Of course if you wish different code path on server and client you should use this trick on some API facade module.

guidsen commented 8 years ago

@langpavel Do you want to help building this data fetching in this repo? I see react-starter-kit is also using universal-router https://github.com/kriasoft/react-starter-kit/blob/master/src/server.js#L19.

If I'm correct this is just the functionality to make sure both the server will wait for the promise to resolve before rendering the first page.

langpavel commented 8 years ago

I'm not sure how much functionality must be done, I'll try to watch on this more deeply when I have enough time