Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs with redux

I have been reading some on Redux. Most of what I read is combining redux with react. I use Angularjs. Is there a good reason to use Redux vs just managing state within angularjs scope and allowing angular to manage the bindings

like image 781
David Champion Avatar asked Jan 20 '16 15:01

David Champion


People also ask

Can we use Redux in AngularJS?

To use Redux in the Angular framework, we can use the NgRx library. This is a reactive state management library. With NgRx, we can get all events (data) from the Angular app and put them all in the same place (Store).

Is AngularJS still used in 2021?

Released by Google in 2010, AngularJS reached end-of-life (EOL) status on December 31, 2021. It means the long-term support will be ceased from the community.

Can I use Redux with JavaScript?

Redux is a predictable state container designed to help you write JavaScript apps that behave consistently across client, server, and native environments and are easy to test. While it's mostly used as a state management tool with React, you can use it with any other JavaScript framework or library.

Can we use AngularJS With React?

Although you can use ngReact, you also can create directives easily to make use of React. In Angular, directives are the best way to integrate "View Oriented" components to your angular application, and React is one framework much oriented to presentation/view layer.


1 Answers

Redux has benefits with Angular 1.x if you have a lot of shared state. In the Angular app I work on we have many pages with a shared model and several components that make (overlapping) changes to that model. It's not always easy to keep that data in sync or to have a standard way for changes to happen. Redux is a nice way to do that, though you could certainly implement something similar just using Angular services. The one-way data flow that Redux uses is easier (in my opinion) to follow than what you normally do in Angular. Conversely, introducing Redux probably hurts your ability to write really quick prototypes, since there's a bit more work to pass data around. That doesn't matter to me, but might matter to others.

The main principles of Redux still apply in Angular apps:

  • Single source of truth (single state object). Like I said above, I think it's easier to manage state when it's in a single place versus managed in different Angular scopes or services. Scope-soup is a real problem in some apps.
  • State is immutable. This one is where most of the friction with Angular comes in, since Angular (and Javascript) make mutating data really easy. But it does help you write safer code. Since you can only change an immutable object by creating a copy, you can be more confident any data manipulation you're doing in a directive isn't going to break other directives. And conversely, you can expect that other directives aren't making modifications to your data. All of those modifications go through a central place (the single state object, via reducers), so it's easier to find them.
  • Changes are made by pure functions. I think this is useful in any JS application. The more code you have that doesn't have a bunch of side effects or framework dependencies, the easier your app is to understand and test. A lot of our tests for Angular code have a bunch of app setup boilerplate. Testing a reducer is incredibly simple by comparison, since it's just a Javascript function.

Using Redux means less of your code is Angular-centric. This means you have an easier upgrade path if you decide to not use Angular. Or even if you just want to migrate to Angular 2. How much easier is hard to say, though.

I don't think there's a ton of overlap due to Angular being more of a framework than a library, but there are things in Redux you can't take as much advantage of in Angular. You might know precisely what part of your app state is changing, but Angular is going run its digest cycle and check everything anyway. Angular 2 is better in that regard, though. You're going to have to jump through some hoops to make all your directives work with immutable data. And especially if you want to send every field a user changes through the Redux store, since ng-model wants to mutate the property you pass it.

Every app is different, but using Redux makes sense in the type of Angular apps I've worked on.

like image 171
JeffB Avatar answered Sep 20 '22 02:09

JeffB