Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

reactjs - Difference between : Redux and Relay

I have read number of articles/docs based on redux and relay but still I am confused how this two libraries are different?
What are the advantages and downsides of this this two libraries?
What is the exact role of GraphQL in relay?
Which is the library more suitable for enterprise data driven applications like CRM/ERP?

question from:https://stackoverflow.com/questions/38036543/difference-between-redux-and-relay

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Both are Flux implementations, a Facebook framework for managing application's state.

  • Redux: general javascript library which helps handle state management in your application. Redux is not react-dependent and can be used with any library. The react-redux library is used to easily integrate react with redux. In redux the application state is located in a single store, each component can access the state, and can also change the state by dispatching actions. Redux doesn't handle data fetching out of the box, though it can be done manually: simply create an action that fetches the data from the server into the store.

  • Relay: Created by facebook for react, and also used internally there. Relay is similar to redux in that they both use a single store. The main difference is that relay only manages state originated from the server, and all access to the state is used via GraphQL querys (for reading data) and mutations (for changing data). Relay caches the data for you and optimizes data fetching for you, by fetching only changed data and nothing more. Relay also supports optimistic updates, i.e. changing the state before the server's result arrives.

GraphQL is a web service framework and protocol using declarative and composable queries, and solves problem like over fetching and under fetching, it is believed to be a valid candidate to replace REST.
GraphQL is not relay dependent, other way around, relay depends on graphql. Graphql can be used in redux same way every other data fetching is done.

As you can see the main advantage of relay over redux is that data fetching is already taken care of, and very optimized for that.
On the other hand, it can't manage client's specific state, but that is rarely needed.

Also, IMO Relay is harder to learn and implement, but the end result is better and more optimized, but for small applications I'd go with redux.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...