Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Flux (etc.) with React?

I've been working on a fairly simple React app as a learning process, and want to begin incorporating Flux (or, more likely, Redux) to continue the education.

The code to implement a Flux-alike solution seems fairly straightforward, but I'm a little unclear on when it's appropriate and how things should be arranged in a best-practice sense.

In ultra layman terms, my best guess at a common use case is that Flux allows components to talk to each other without having common props passed around, so with that in mind, here's an example of something I would hope to use Flux for in my app:

Let's say I have a user profile component which periodically refreshes via an ajax call. While this call is waiting for its data to be returned, a loading spinner gif displays elsewhere on the page - not in a parent or child of the component that fires the ajax call. In this scenario, how would I use Flux to prompt the spinner to display/hide at the appropriate moments (i.e. begin with the request is sent, end when a response is received)? If its visibility were defined by the profile component's state, that's easy, but obviously I need one component to communicate with what I suppose you could call a distant cousin.

How would I lay out my actions, reducers and whatnot to achieve this?

I'm not looking for anyone to write code for me here, as I'll definitely learn that better by doing it, but I'd definitely appreciate some general advice on the methodology to be used in such a situation.

Many thanks!

like image 443
user1381745 Avatar asked May 27 '16 07:05

user1381745


People also ask

What could be a reason to use flux in React?

Flux is a Javascript architecture or pattern for UI which runs on a unidirectional data flow and has a centralized dispatcher. It is useful when your project has dynamic data and you need to keep the data updated in an effective manner. It was created by Facebook, and complements React as view.

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 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 meant by flux in React?

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.


1 Answers

Redux basically gives you global state that any component can hook into.

So for your ajax call you'd have 3 actions. Check out the Redux docs for creating async actions.

GET_USER_PROFILE_START
GET_USER_PROFILE_SUCCESS
GET_USER_PROFILE_ERROR

Then you'll have reducers for

userProfile
isUserProfileLoading

When you fire your ajax request, your async action will first fire your GET_USER_PROFILE_START action, which will be picked up by your isUserProfileLoading reducer and will set isUserProfileLoading to true.

Then when the request comes back, if it succeeds, it'll fire GET_USER_PROFILE_SUCCESS with a payload of userProfile. Your userProfile reducer will receive userProfile and set it into state.

Your isUserProfileLoading reducer will also be listening for GET_USER_PROFILE_SUCCESS and will set isUserProfileLoading back to false when it sees that action.

If your request fails, you fire a GET_USER_PROFILE_ERROR action with an error payload and do some sort of notification in the UI. And your isUserProfileLoading reducer will also be listening for GET_USER_PROFILE_ERROR and will also set isUserProfileLoading back to false when it sees it.

Now any component in your app, no matter where it is on the page or in the component hierarchy, can look at isUserProfileLoading and render a loading spinner if it's set to true.

Hope that helps.

like image 56
jaybee Avatar answered Oct 06 '22 16:10

jaybee