Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need Flux with React?

I don't understand why we need Flux with React as React itself let's us maintain the state of the application. Every component has an initial state and the state can be changed by user actions or any other asynchronous JavaScript.

Why is React called as only a view library when it can let's us define state of the application and also update view whenever state changes. This is not what a view does....its what complete MVC does right?

For example: here is an Todo app build only with React and here is an Todo app build with Flux and React.

If we can build the Todo app with React only then why do we need Flux?

like image 500
Narayan Prusty Avatar asked Mar 10 '16 18:03

Narayan Prusty


People also ask

What is the primary advantage of using flux implementation in React?

Main Features of Flux Flux keeps code predictable when compared to other MVC frameworks. Developers can build applications without being bothered about complicated interactions between data resources. Flux boasts of a better structured data flow – unidirectional. Being unidirectional is the central feature of Flux.

What is the purpose of a flux store?

In Flux, Stores are simply a place where data is read out from. More specifically, Views within a Flux architecture will be notified of changes within Stores via the Observer pattern, and then query for those data in order to update their own states.

Should I use flux or Redux?

The primary difference of Flux vs Redux is that Flux includes multiple Stores per app, but Redux includes a single Store per app. Rather than placing state information in multiple Stores across the application, Redux keeps everything in one region of the app.

What are flux element in react JS?

Flux is a programming concept, where the data is uni-directional. This data enters the app and flows through it in one direction until it is rendered on the screen.


2 Answers

In theory you don't need flux. In small applications you don't need flux for sure. But what if your application consist of hundreds components? And one of your component is form. User populate this form and you send its content to server. And get response from server with new data. And assume that this response data and data from form are necessary to other components.

  1. Without flux: You can move your data to root component and then distribute it down to all components. But if you need to distribute data from many other components too? This makes your application very complex.

  2. with flux: You move your data to stores, and all components which are interested about this data, can get it from there. You have better control on your application and source data.

I prefer redux (only one store and one source of truth)

edit:

Why is React called as a view library even if it can handle application state?

MVC is a software architectural pattern. It divides a given software application into three interconnected parts (models, views, controllers). If we think about react and MVC it fit as View. But this is nothing wrong. It doesn't mean that you can use it only for views. It allows you to create normal applications.

But in other hand you can use it as view for other frameworks (for example you can use it with angular).

In other words it is very flexible library for many uses.

like image 180
Krzysztof Sztompka Avatar answered Sep 18 '22 23:09

Krzysztof Sztompka


You don't NEED Flux the same you don't need MVC. They are both architectures and you can of course build something without using either.

Would you build a non-MVC app in 2016? Probably not, that doesn't mean that people didn't do it in the past.

Flux is awesome! But as most things in the tech industry is not always the right decision, things vary in a project requirement basis.

Probably the biggest selling point of Flux is that it tries to enforce the data flow in one direction, this means that you know for sure where the data is coming from. In a non-flux app the data for a component could be an own property, a property passed down the component tree, a local state variable, a state variable result of calling an API.

With Flux: "where does the data come from?". Answer: from the stores. Redux takes this further and only uses a single store.

Flux has been criticized because you need a lot of boilerplate code but again is a matter of tradeoffs.

At the end is always your call depending on your project needs.

like image 29
nbermudezs Avatar answered Sep 16 '22 23:09

nbermudezs