What is Redux
Redux is an open-source JavaScript library that helps you to manage the state of your application. For Building User Interface this is used with libraries for example React or Angular.
Redux may be a tiny library with an easy, restricted API designed to be a inevitable instrumentality for application state. It operates during a similar fashion to a reducing operate, a purposeful programming conception.
It offers following things:
• A single, immutable knowledge store.
• One-way knowledge flow.
• An approach to vary supported pure functions and a stream of actions.
Benefits of using Redux :-
When To Use Redux :
Building Blocks Of Redux :-
There are three building blocks of redux :-
Pure Functions: –
A function returns constant result for same arguments. Its analysis has no aspect effects, i.e., it doesn’t alter input file. It doesn’t rely upon the external state sort of a global variable.
Example: –
Function reducer(state,action){
switch(action.type){
Case ‘INCREMENT’ :
return{ count : state.count + 1 };
}
}
Benefits of Pure Functions :-
@angular-redux/store includes a peer dependency on redux,Because of that we must have to install that dependency.
npm install –save redux @angular-redux/store
Import the NgReduxModule class and add it to your application module as an import. Once you’ve done this, you’ll be able to inject NgRedux into your Angular components.
import { NgReduxModule, NgRedux } from ‘@angular-redux/store’;
import { createLogger } from ‘redux-logger’;
import { rootReducer } from ‘./reducers’;
interface IAppState {
/* … */
}
@NgModule({
/* … */
imports: [, /* … */ NgReduxModule],
})
export class AppModule {
constructor(ngRedux: NgRedux<IAppState>) {
ngRedux.configureStore(rootReducer, {}, [createLogger()]);
}
}
Now You installed Redux in your Angular Application! Use the @select decorator to access your store state, and .dispatch() to dispatch actions:
import{select}from'@angular-redux/store';
@Component{
:
'<button (click)="onClick()">Clicked {{ count | async }} times</button>',
}
classApp{
@select:Observable<number>;
constructor(privatengRedux:NgRedux<IAppState>){}
(){
this.ngRedux.dispatch{:INCREMENT};
}
}
Source :- https://www.npmjs.com/package/@angular-redux/store
https://blog.logrocket.com/why-use-redux-reasons-with-clear-examples-d21bffd5835/