Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/immutable invariant #381

Merged
merged 12 commits into from
Feb 21, 2020
7 changes: 5 additions & 2 deletions docs/api/getDefaultMiddleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ One of the goals of Redux Toolkit is to provide opinionated defaults and prevent
`getDefaultMiddleware` includes some middleware that are added **in development builds of your app only** to
provide runtime checks for two common issues:

- [`redux-immutable-state-invariant`](https://github.com/leoasis/redux-immutable-state-invariant): deeply compares
- [`immutable-state-invariant`](./otherExports.md#createimmutablestateinvariantmiddleware): deeply compares
state values for mutations. It can detect mutations in reducers during a dispatch, and also mutations that occur between
dispatches (such as in a component or a selector). When a mutation is detected, it will throw an error and indicate the key
path for where the mutated value was detected in the state tree.

Forked from [`redux-immutable-state-invariant`](https://github.com/leoasis/redux-immutable-state-invariant)

- [`serializable-state-invariant-middleware`](./otherExports.md#createserializablestateinvariantmiddleware): a custom middleware created specifically for use in Redux Toolkit. Similar in
concept to `redux-immutable-state-invariant`, but deeply checks your state tree and your actions for non-serializable values
concept to `immutable-state-invariant`, but deeply checks your state tree and your actions for non-serializable values
such as functions, Promises, Symbols, and other non-plain-JS-data values. When a non-serializable value is detected, a
console error will be printed with the key path for where the non-serializable value was detected.

Expand Down
50 changes: 50 additions & 0 deletions docs/api/otherExports.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,56 @@ Redux Toolkit exports some of its internal utilities, and re-exports additional

## Internal Exports

### `createImmutalStateInvariantMiddleware`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we release 1.3 we should probably extract these into their own docs page or something, but good enough for now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, typo here: createImmutalState -> createImmutableState


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

Accepts a single configuration object parameter, with the following options:

```ts
function createImmutableStateInvariantMiddleware({
// The function to check if a value is considered to be immutable.
// This function is applied recursively to every value contained in the state.
// The default implementation will return true for primitive types (like numbers, strings, booleans, null and undefined).
isImmutable?: (value: any) => boolean
// An array of dot-separated path strings that match named nodes from the root state to ignore when checking for immutability.
// Defaults to undefined
ignoredPaths?: string[]
})
```

Example:

```js
import {
createSlice,
configureStore,
createImmutableStateInvariantMiddleware
} from '@reduxjs/toolkit'

const exampleSlice = createSlice({
name: 'example',
initialState: {
user: 'will track changes',
ignoredPath: 'single level',
ignoredNested: {
one: 'one',
two: 'two'
}
},
reducers: {}
})

const immutableInvariantMiddleware = createImmutableStateInvariantMiddleware({
ignoredPaths: ['ignoredPath', 'ignoredNested.one', 'ignoredNested.two']
})

const store = configureStore({
reducer: exampleSlice.reducer,
middleware: [immutableInvariantMiddleware]
})
```

### `createSerializableStateInvariantMiddleware`

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