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

Docs: Add simpler Basic Usage example for createListener Middleware (suggestion) #4836

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions docs/api/createListenerMiddleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@ Listeners can be defined statically by calling `listenerMiddleware.startListenin

### Basic Usage

```js
import { configureStore, createListenerMiddleware } from '@reduxjs/toolkit'
import todosReducer, { todoAdded } from '../features/todos/todosSlice'

// Create the middleware instance and methods:
const listenerMiddleware = createListenerMiddleware()

// Add one or more listener entries that look for specific actions.
// They may contain any sync or async logic, similar to thunks.
listenerMiddleware.startListening({
actionCreator: todoAdded,
effect: async (action, listenerApi) => {
// Run whatever additional side-effect-y logic you want here

console.log('Todo added: ', action.payload.text)
console.log('State: ', listenerApi.getState())

// Dispatch some action:
listenerApi.dispatch({ type: "SOME_TYPE", payload: "SOME_PAYLOAD" }) // You can use action creators of course

}
},
})

const store = configureStore({
reducer: {
todos: todosReducer,
},
// Add the listener middleware to the store.
// NOTE: Since this can receive actions with functions inside,
// it should go before the serializability check middleware
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().prepend(listenerMiddleware.middleware),
})
```

### Extended Usage

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

Expand All @@ -32,15 +70,12 @@ import todosReducer, {
todoDeleted,
} from '../features/todos/todosSlice'

// Create the middleware instance and methods
const listenerMiddleware = createListenerMiddleware()

// Add one or more listener entries that look for specific actions.
// They may contain any sync or async logic, similar to thunks.
listenerMiddleware.startListening({
actionCreator: todoAdded,
effect: async (action, listenerApi) => {
// Run whatever additional side-effect-y logic you want here

console.log('Todo added: ', action.payload.text)

// Can cancel other running instances
Expand Down Expand Up @@ -73,16 +108,13 @@ listenerMiddleware.startListening({
},
})

const store = configureStore({
reducer: {
todos: todosReducer,
},
// Add the listener middleware to the store.
// NOTE: Since this can receive actions with functions inside,
// it should go before the serializability check middleware
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().prepend(listenerMiddleware.middleware),
})
// Add another listener:
listenerMiddleware.startListening({
actionCreator: todoDelete,
...
});

const store = configureStore(...)
```

## `createListenerMiddleware`
Expand Down