Skip to content

Commit 6200338

Browse files
committed
Merge branch 'master' into create-slice-creators
2 parents b299631 + ed8282e commit 6200338

File tree

8 files changed

+83
-17
lines changed

8 files changed

+83
-17
lines changed

docs/api/createSlice.mdx

+2-3
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,12 @@ Instead, import `buildCreateSlice` and `asyncThunkCreator`, and create your own
255255
```ts
256256
import { buildCreateSlice, asyncThunkCreator } from '@reduxjs/toolkit'
257257
258-
// name is up to you
259-
export const createSliceWithThunks = buildCreateSlice({
258+
export const createAppSlice = buildCreateSlice({
260259
creators: { asyncThunk: asyncThunkCreator },
261260
})
262261
```
263262

264-
Then import this `createSlice` as needed instead of the exported version from RTK.
263+
Then import this `createAppSlice` as needed instead of the exported version from RTK.
265264

266265
:::
267266

docs/usage/migrating-rtk-2.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,11 @@ In practice, we hope these are reasonable tradeoffs. Creating thunks inside of `
657657
Here's what the new callback syntax looks like:
658658

659659
```ts
660-
const createSliceWithThunks = buildCreateSlice({
660+
const createAppSlice = buildCreateSlice({
661661
creators: { asyncThunk: asyncThunkCreator },
662662
})
663663

664-
const todosSlice = createSliceWithThunks({
664+
const todosSlice = createAppSlice({
665665
name: 'todos',
666666
initialState: {
667667
loading: false,

packages/rtk-codemods/package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reduxjs/rtk-codemods",
3-
"version": "0.0.3",
3+
"version": "0.1.0",
44
"scripts": {
55
"lint": "eslint --cache .",
66
"test": "vitest",
@@ -39,5 +39,9 @@
3939
},
4040
"publishConfig": {
4141
"access": "public"
42+
},
43+
"repository": {
44+
"type": "git",
45+
"url": "git+https://github.com/reduxjs/redux-toolkit.git"
4246
}
4347
}

packages/rtk-query-graphql-request-base-query/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rtk-query/graphql-request-base-query",
3-
"version": "2.3.0",
3+
"version": "2.3.1",
44
"author": {
55
"name": "Lenz Weber",
66
"email": "mail@phryneas.de",
@@ -27,7 +27,7 @@
2727
"graphql-request": "^4.0.0 || ^5.0.0 || ^6.0.0"
2828
},
2929
"peerDependencies": {
30-
"@reduxjs/toolkit": "^1.7.1",
30+
"@reduxjs/toolkit": "^1.7.1 || ^2.0.0",
3131
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
3232
},
3333
"devDependencies": {

packages/toolkit/src/query/react/buildHooks.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,12 @@ export function buildHooks<Definitions extends EndpointDefinitions>({
915915
(_: ApiRootState, lastResult: any) => lastResult,
916916
(_: ApiRootState) => stableArg,
917917
],
918-
queryStatePreSelector
918+
queryStatePreSelector,
919+
{
920+
memoizeOptions: {
921+
resultEqualityCheck: shallowEqual,
922+
},
923+
}
919924
),
920925
[select, stableArg]
921926
)

packages/toolkit/src/query/tests/buildHooks.test.tsx

+60-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import { server } from './mocks/server'
3535
import type { UnknownAction } from 'redux'
3636
import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState'
3737
import type { SerializedError } from '@reduxjs/toolkit'
38-
import { createListenerMiddleware, configureStore } from '@reduxjs/toolkit'
38+
import { createListenerMiddleware, configureStore, createSlice } from '@reduxjs/toolkit'
3939
import { delay } from '../../utils'
4040
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
4141
import { countObjectKeys } from '../utils/countObjectKeys'
@@ -2052,7 +2052,19 @@ describe('hooks with createApi defaults set', () => {
20522052
}),
20532053
})
20542054

2055-
const storeRef = setupApiStore(api)
2055+
const counterSlice = createSlice({
2056+
name: "counter",
2057+
initialState: { count: 0 },
2058+
reducers: {
2059+
increment(state) {
2060+
state.count++
2061+
}
2062+
}
2063+
})
2064+
2065+
const storeRef = setupApiStore(api, {
2066+
counter: counterSlice.reducer,
2067+
})
20562068

20572069
expectExactType(api.useGetPostsQuery)(api.endpoints.getPosts.useQuery)
20582070
expectExactType(api.useUpdatePostMutation)(
@@ -2317,6 +2329,52 @@ describe('hooks with createApi defaults set', () => {
23172329
await waitFor(() => expect(getRenderCount()).toBe(3))
23182330
})
23192331

2332+
test('useQuery with selectFromResult option does not update when unrelated data in the store changes', async () => {
2333+
function Posts() {
2334+
const { posts } = api.endpoints.getPosts.useQuery(undefined, {
2335+
selectFromResult: ({ data }) => ({
2336+
// Intentionally use an unstable reference to force a rerender
2337+
posts: data?.filter((post) => post.name.includes('post')),
2338+
}),
2339+
})
2340+
2341+
getRenderCount = useRenderCounter()
2342+
2343+
return (
2344+
<div>
2345+
{posts?.map((post) => (
2346+
<div key={post.id}>{post.name}</div>
2347+
))}
2348+
</div>
2349+
)
2350+
}
2351+
2352+
function CounterButton() {
2353+
return (
2354+
<div
2355+
data-testid="incrementButton"
2356+
onClick={() => storeRef.store.dispatch(counterSlice.actions.increment())}
2357+
>
2358+
Increment Count
2359+
</div>
2360+
)
2361+
}
2362+
2363+
render(
2364+
<div>
2365+
<Posts />
2366+
<CounterButton />
2367+
</div>,
2368+
{ wrapper: storeRef.wrapper }
2369+
)
2370+
2371+
await waitFor(() => expect(getRenderCount()).toBe(2))
2372+
2373+
const incrementBtn = screen.getByTestId('incrementButton')
2374+
fireEvent.click(incrementBtn)
2375+
expect(getRenderCount()).toBe(2)
2376+
})
2377+
23202378
test('useQuery with selectFromResult option has a type error if the result is not an object', async () => {
23212379
function SelectedPost() {
23222380
const _res1 = api.endpoints.getPosts.useQuery(undefined, {

packages/toolkit/src/tests/createSlice.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ describe('createSlice', () => {
631631
'"create.asyncThunk is not a function"'
632632
)
633633
})
634-
const createThunkSlice = buildCreateSlice({
634+
const createAppSlice = buildCreateSlice({
635635
creators: { asyncThunk: asyncThunkCreator },
636636
})
637637
function pending(state: any[], action: any) {
@@ -648,7 +648,7 @@ describe('createSlice', () => {
648648
}
649649

650650
test('successful thunk', async () => {
651-
const slice = createThunkSlice({
651+
const slice = createAppSlice({
652652
name: 'test',
653653
initialState: [] as any[],
654654
reducers: (create) => ({
@@ -691,7 +691,7 @@ describe('createSlice', () => {
691691
})
692692

693693
test('rejected thunk', async () => {
694-
const slice = createThunkSlice({
694+
const slice = createAppSlice({
695695
name: 'test',
696696
initialState: [] as any[],
697697
reducers: (create) => ({
@@ -735,7 +735,7 @@ describe('createSlice', () => {
735735
})
736736

737737
test('with options', async () => {
738-
const slice = createThunkSlice({
738+
const slice = createAppSlice({
739739
name: 'test',
740740
initialState: [] as any[],
741741
reducers: (create) => ({
@@ -784,7 +784,7 @@ describe('createSlice', () => {
784784
})
785785

786786
test('has caseReducers for the asyncThunk', async () => {
787-
const slice = createThunkSlice({
787+
const slice = createAppSlice({
788788
name: 'test',
789789
initialState: [],
790790
reducers: (create) => ({

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -7254,7 +7254,7 @@ __metadata:
72547254
rimraf: ^3.0.2
72557255
typescript: ^4.3.4
72567256
peerDependencies:
7257-
"@reduxjs/toolkit": ^1.7.1
7257+
"@reduxjs/toolkit": ^1.7.1 || ^2.0.0
72587258
graphql: ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0
72597259
languageName: unknown
72607260
linkType: soft

0 commit comments

Comments
 (0)