All Projects → nartc → ngrx-slice

nartc / ngrx-slice

Licence: MIT license
Moved to https://github.com/nartc/nartc-workspace

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ngrx-slice

ngrx-etc
Utility functions for NgRx
Stars: ✭ 70 (+40%)
Mutual labels:  ngrx, immer
angular2-webpack-advance-starter
An advanced Angular2 Webpack Starter project with support for ngrx/store, ngrx/effects, ng2-translate, angulartics2, lodash, NativeScript (*native* mobile), Electron (Mac, Windows and Linux desktop) and more.
Stars: ✭ 49 (-2%)
Mutual labels:  ngrx
devto-clone
This is a dev to clone in angular. Where we used Angular component-store, rx-angular/template
Stars: ✭ 36 (-28%)
Mutual labels:  ngrx
Angular-Ecommerce-App-with-NGRX
No description or website provided.
Stars: ✭ 37 (-26%)
Mutual labels:  ngrx
angular-plugins
Reactive Angular Plugins
Stars: ✭ 36 (-28%)
Mutual labels:  ngrx
streamkit
My streaming overlay platform for YouTube https://bit.ly/3AvaoFz and Twitch https://bit.ly/37xUPAM
Stars: ✭ 15 (-70%)
Mutual labels:  ngrx
rematch
The Redux Framework
Stars: ✭ 8,340 (+16580%)
Mutual labels:  immer
angular-cli-skeleton
angular-cli skeleton to quickly start a new project with advanced features and best practices. All features are described in README.md.
Stars: ✭ 32 (-36%)
Mutual labels:  ngrx
elf-taro
Taro 小程序 脚手架 (Taro 3 + Dva + Typescript + Immer) - 内置 Redux 计数器, 异步数据请求 与 腾讯小程序地图 demo
Stars: ✭ 61 (+22%)
Mutual labels:  immer
undox
⎌ Redux Implementation of Undo/Redo based on storing actions instead of states.
Stars: ✭ 25 (-50%)
Mutual labels:  ngrx
nstate
A simple but powerful react state management library with low mind burden
Stars: ✭ 11 (-78%)
Mutual labels:  immer
ionic3-firebase-ngrx
Sample Ionic 3 application using ngrx with firebase (auth, crud and camera plugin)
Stars: ✭ 48 (-4%)
Mutual labels:  ngrx
mean-ionic-ngrx
Monorepo as Microservices: Full MEAN stack with Angular 7, Ionic 4 and ReactiveX API (ngrx/Store + ngrx/Effects) + i18n/ngx-translate + Express NodeJS REST API servers micro-services + JWT Authentication + UnitTest/e2e exemple + Travis + DevOps scripts and many more...
Stars: ✭ 60 (+20%)
Mutual labels:  ngrx
ngrx-store-ionic-storage
Simple syncing between @ngrx/store and Ionic Storage.
Stars: ✭ 66 (+32%)
Mutual labels:  ngrx
react-immer
No nonsense state management with Immer and React hooks
Stars: ✭ 13 (-74%)
Mutual labels:  immer
material2-admin
Angular - Material2 - Redux - Flex - Admin Example - Starter
Stars: ✭ 13 (-74%)
Mutual labels:  ngrx
ngrx-films-list
Ngrx example app
Stars: ✭ 15 (-70%)
Mutual labels:  ngrx
ionic4-boilerplate
🚀 boilerplate for ionic4 with CI based on travis and fastlane. doc and example are provided
Stars: ✭ 25 (-50%)
Mutual labels:  ngrx
todo-app-ngrx
TodoMV* - Angular + Redux (ngrx)
Stars: ✭ 42 (-16%)
Mutual labels:  ngrx
spring-kotlin-angular4
Another abadoned Spring Boot application with Angular and Kotlinabandoned
Stars: ✭ 22 (-56%)
Mutual labels:  ngrx

Moved to https://github.com/nartc/nartc-workspace

ngrx-slice

Netlify Status

ngrx-slice is a plugin that intends to provide the same functionalities that Redux Toolkit createSlice provides. It is meant to be opinionated.

Installation

npm install ngrx-slice
yarn add ngrx-slice

Peer Dependencies

ngrx-slice has ngrx-immer and immer as its peerDependencies so go ahead and install those:

npm install ngrx-immer immer
yarn add ngrx-immer immer

Here's one command for all three:

npm install ngrx-slice ngrx-immer immer
yarn add ngrx-slice ngrx-immer immer

Documentations

Visit NgRX Slice Documentations

Why

NgRX has always been coupled with boilerplate. Even with the new Creator APIs, the amount of boilerplate needed to set up a single feature state is still a lot (to remember). To fully utilize NgRX for a Feature State, you'd need:

  • Actions (createAction)
  • Selectors (createSelector and createFeatureSelector)
  • Reducer (createReducer)

Regardless of whether you separate these entities into different files, or keep them in the same file, the cognitive overhead is still there. ngrx-slice solves this issue for me.

Here's an example of a CounterState using createAction, createSelector, createFeatureSelector, and createReducer

// Actions
const increment = createAction("[Counter] increment");
const decrement = createAction("[Counter] decrement");
const double = createAction("[Counter] double");
const multiplyBy = createAction(
  "[Counter] multiplyBy",
  props<{ multiplier: number }>()
);
const multiplyBySuccess = createAction(
  "[Counter] multiplyBy success",
  props<{ value: number }>()
);

// Reducer
interface CounterState {
  value: number;
  increment: number;
  decrement: number;
}

const initialState: CounterState = {
  value: 0,
  increment: 0,
  decrement: 0,
};

const counterReducer = createReducer(
  initialState,
  on(increment, (state) => ({
    ...state,
    value: state.value + 1,
    increment: state.increment + 1,
  })),
  on(decrement, (state) => ({
    ...state,
    value: state.value - 1,
    decrement: state.decrement + 1,
  })),
  on(multiplyBySuccess, (state, action) => ({ ...state, value: action.value })),
  on(double, (state) => ({ ...state, value: state.value * 2 }))
);

// Selectors
const selectCounterState = createFeatureSelector("counter");
const selectValue = createSelector(selectCounterState, (state) => state.value);
const selectIncrement = createSelector(
  selectCounterState,
  (state) => state.increment
);
const selectDecrement = createSelector(
  selectCounterState,
  (state) => state.decrement
);

There is an Effect that will handle multiplyBy action but this will be the same for ngrx-slice as well.

Or you can have everything in a Slice

import { createSlice } from 'ngrx-slice';

export interface CounterState {
  value: number;
  incrementCount: number;
  decrementCount: number;
}

export const initialState: CounterState = {
  decrementCount: 0,
  incrementCount: 0,
  value: 0,
};

export const {
  actions: CounterActions,
  selectors: CounterSelectors,
  ...CounterFeature
} = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      state.value++;
      state.incrementCount++;
    },
    decrement: (state) => {
      state.value--;
      state.decrementCount++;
    },
  },
});

Contribution

Contributions welcome

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].