React + Redux with a rest api?

2019-03-25 17:36发布

问题:

I have a simple CRUD app I am building in node, and have finished creating the rest API in Express. I am now looking to add front end functionality and wish to use react + redux as a learning exercise. It seems however all of the tutorials around this access the data directly using Redux, rather than interfacing with an internal API.

My question is, is that the correct way to build a SPA with redux? I was under the impression it was best to separate the front end from the backend so that I could, for example, build an iPhone app and not have to rebuild the backend.

Thanks in advance.

回答1:

Redux itself has nothing to do with API communication, it's the library for client-side state management. You can use whatever approach for firing and handling requests, most common are using custom api-connecting middleware, returning functions, which fire requests, from action-creators and using them with thunk-middleware or using alternative approaches like rx-bridges or sagas.



回答2:

Start with: https://egghead.io/series/getting-started-with-redux

Reffer to: What is best practice to communicate between React components and services?

Redux by design forces to build SPA application.



回答3:

First, see examples from redux docs, "Real World" is most useful: https://github.com/reactjs/redux/tree/master/examples/real-world

Then, you can research useful utils like redux-promise-middleware.

Finally, you can try to use some complex utils for connect your redux-app with API. redux-rest-adapter is one of them.