Skip to content

Commit 6b83a39

Browse files
msutkowskitgoualamarkeriksonlex111
authored
Feature/immutable invariant (#381)
* strongly type slice name (#354) * strongly type slice name * move new generic to the end and default it to string * Remove accidental yarn.lock * Update DS to fix sidebar bug * Update Docusaurus and add lockfile to 43 version (#369) * Update Docusaurus and add lockfile to 43 version * Fix lockfile * Update netlify.toml to remove Yarn command * Try forcing node version Co-authored-by: Mark Erikson <[email protected]> * Try adding the compressed-size-action (#372) * Port redux-immutable-invariant and update docs * Update lock * Keep immutable middleware interface types during build * Readd lock file * Add mention of being a fork of redux-immutable-state-invariant * Markdown formatting Co-authored-by: Thibault Gouala <[email protected]> Co-authored-by: Mark Erikson <[email protected]> Co-authored-by: Alexey Pyltsyn <[email protected]>
1 parent 3bb7d8c commit 6b83a39

7 files changed

+830
-76
lines changed

docs/api/getDefaultMiddleware.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,15 @@ One of the goals of Redux Toolkit is to provide opinionated defaults and prevent
5555
`getDefaultMiddleware` includes some middleware that are added **in development builds of your app only** to
5656
provide runtime checks for two common issues:
5757

58-
- [`redux-immutable-state-invariant`](https://github.com/leoasis/redux-immutable-state-invariant): deeply compares
58+
- [`immutable-state-invariant`](./otherExports.md#createimmutablestateinvariantmiddleware): deeply compares
5959
state values for mutations. It can detect mutations in reducers during a dispatch, and also mutations that occur between
6060
dispatches (such as in a component or a selector). When a mutation is detected, it will throw an error and indicate the key
6161
path for where the mutated value was detected in the state tree.
62+
63+
Forked from [`redux-immutable-state-invariant`](https://github.com/leoasis/redux-immutable-state-invariant)
64+
6265
- [`serializable-state-invariant-middleware`](./otherExports.md#createserializablestateinvariantmiddleware): a custom middleware created specifically for use in Redux Toolkit. Similar in
63-
concept to `redux-immutable-state-invariant`, but deeply checks your state tree and your actions for non-serializable values
66+
concept to `immutable-state-invariant`, but deeply checks your state tree and your actions for non-serializable values
6467
such as functions, Promises, Symbols, and other non-plain-JS-data values. When a non-serializable value is detected, a
6568
console error will be printed with the key path for where the non-serializable value was detected.
6669

docs/api/otherExports.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,56 @@ Redux Toolkit exports some of its internal utilities, and re-exports additional
1111

1212
## Internal Exports
1313

14+
### `createImmutalStateInvariantMiddleware`
15+
16+
Creates an instance of the `immutable-state-invariant` middleware described in [`getDefaultMiddleware`](./getDefaultMiddleware.md).
17+
18+
Accepts a single configuration object parameter, with the following options:
19+
20+
```ts
21+
function createImmutableStateInvariantMiddleware({
22+
// The function to check if a value is considered to be immutable.
23+
// This function is applied recursively to every value contained in the state.
24+
// The default implementation will return true for primitive types (like numbers, strings, booleans, null and undefined).
25+
isImmutable?: (value: any) => boolean
26+
// An array of dot-separated path strings that match named nodes from the root state to ignore when checking for immutability.
27+
// Defaults to undefined
28+
ignoredPaths?: string[]
29+
})
30+
```
31+
32+
Example:
33+
34+
```js
35+
import {
36+
createSlice,
37+
configureStore,
38+
createImmutableStateInvariantMiddleware
39+
} from '@reduxjs/toolkit'
40+
41+
const exampleSlice = createSlice({
42+
name: 'example',
43+
initialState: {
44+
user: 'will track changes',
45+
ignoredPath: 'single level',
46+
ignoredNested: {
47+
one: 'one',
48+
two: 'two'
49+
}
50+
},
51+
reducers: {}
52+
})
53+
54+
const immutableInvariantMiddleware = createImmutableStateInvariantMiddleware({
55+
ignoredPaths: ['ignoredPath', 'ignoredNested.one', 'ignoredNested.two']
56+
})
57+
58+
const store = configureStore({
59+
reducer: exampleSlice.reducer,
60+
middleware: [immutableInvariantMiddleware]
61+
})
62+
```
63+
1464
### `createSerializableStateInvariantMiddleware`
1565

1666
Creates an instance of the `serializable-state-invariant` middleware described in [`getDefaultMiddleware`](./getDefaultMiddleware.md).

0 commit comments

Comments
 (0)