For Angular 1 see ng-redux
ngRedux lets you easily connect your angular components with Redux.
npm install --save ng2-reduximport {bootstrap} from 'angular2/angular2';
import {bind} from 'angular2/di';
import {createStore, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import {App} from './containers/App';
import {provider} from 'ng2-redux';
import {rootReducer} from './reducers';
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore);
const store = createStoreWithMiddleware(rootReducer);
bootstrap(
App,
[provider(store)]
);import * as CounterActions from '../actions/CounterActions';
class CounterApp {
constructor( @Inject('ngRedux') ngRedux) {
this.unsubscribe = ngRedux.connect(this.mapStateToThis, this.mapDispatchToThis)(this);
}
onInit() {}
onDestroy() {
this.unsubscribe();
}
mapStateToThis(state) {
return {
counter: state.counter
};
}
mapDispatchToThis(dispatch) {
return { actions: bindActionCreators(CounterActions, dispatch) };
}
}Provide the Redux store to connect.
store(Object): Redux's store instance
Connects an Angular component to Redux.
mapStateToTarget(Function): connect will subscribe to Redux store updates. Any time it updates, mapStateToTarget will be called. Its result must be a plain object, and it will be merged intotarget. If you have a component which simply triggers actions without needing any state you can pass null tomapStateToTarget.- [
mapDispatchToTarget] (Object or Function): Optional. If an object is passed, each function inside it will be assumed to be a Redux action creator. An object with the same function names, but bound to a Redux store, will be merged ontotarget. If a function is passed, it will be givendispatch. It’s up to you to return an object that somehow usesdispatchto bind action creators in your own way. (Tip: you may use thebindActionCreators()helper from Redux.).
You then need to invoke the function a second time, with target as parameter:
target(Object or Function): If passed an object, the results ofmapStateToTargetandmapDispatchToTargetwill be merged onto it. If passed a function, the function will receive the results ofmapStateToTargetandmapDispatchToTargetas parameters.
e.g:
connect(this.mapStateToThis, this.mapDispatchToThis)(this);
//Or
connect(this.mapState, this.mapDispatch)((selectedState, actions) => {/* ... */});- The
mapStateToTargetfunction takes a single argument of the entire Redux store’s state and returns an object to be passed as props. It is often called a selector. Use reselect to efficiently compose selectors and compute derived data.
All of redux's store methods (i.e. dispatch, subscribe and getState) are exposed by $ngRedux and can be accessed directly. For example:
ngRedux.subscribe(() => {
let state = $ngRedux.getState();
//...
})This means that you are free to use Redux basic API in advanced cases where connect's API would not fill your needs.
In order to use Redux DevTools with your angular app, you need to install react, react-redux and redux-devtools as development dependencies.
You can find a sample devtools implentation in the counter example