Skip to content

Commit

Permalink
Merge branch 'main' into zhihengGet/main
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlancollins committed Jul 16, 2024
2 parents 016f9f0 + d10a456 commit bb08ee8
Show file tree
Hide file tree
Showing 26 changed files with 455 additions and 286 deletions.
28 changes: 28 additions & 0 deletions docs/framework/react/reference/useMutationState.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,34 @@ const data = useMutationState({
})
```

**Example 3: Access the latest mutation data via the `mutationKey`**
Each invocation of `mutate` adds a new entry to the mutation cache for `gcTime` milliseconds.

To access the latest invocation, you can check for the last item that `useMutationState` returns.

```tsx
import { useMutation, useMutationState } from '@tanstack/react-query'

const mutationKey = ['posts']

// Some mutation that we want to get the state for
const mutation = useMutation({
mutationKey,
mutationFn: (newPost) => {
return axios.post('/posts', newPost)
},
})

const data = useMutationState({
// this mutation key needs to match the mutation key of the given mutation (see above)
filters: { mutationKey },
select: (mutation) => mutation.state.data,
})

// Latest mutation data
const latest = data[data.length - 1]
```

**Options**

- `options`
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/node": "^20.12.12",
"@types/react": "npm:types-react@rc",
"@types/react-dom": "npm:types-react-dom@rc",
"@vitest/coverage-istanbul": "^1.6.0",
"@vitest/coverage-istanbul": "^2.0.2",
"cpy-cli": "^5.0.0",
"esbuild-plugin-file-path-extensions": "^2.1.0",
"eslint": "^8.57.0",
Expand All @@ -75,7 +75,7 @@
"typescript51": "npm:[email protected]",
"typescript52": "npm:[email protected]",
"vite": "^5.2.11",
"vitest": "^1.6.0"
"vitest": "^2.0.2"
},
"pnpm": {
"overrides": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('lazyInit', () => {

test('should init lazily and only once', async () => {
const initCallFn = vi.fn()
const registerDataValue = vi.fn<[number]>()
const registerDataValue = vi.fn<(arg: number) => any>()

let value!: { data: WritableSignal<number> }
const outerSignal = signal(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('lazySignalInitializer', () => {

test('should init lazily and only once', async () => {
const initCallFn = vi.fn()
const registerEffectValue = vi.fn<[number]>()
const registerEffectValue = vi.fn<(arg: number) => any>()

let value!: Signal<number>
const outerSignal = signal(0)
Expand Down
6 changes: 4 additions & 2 deletions packages/query-core/src/__tests__/hydration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ describe('dehydration and rehydration', () => {
key: [{ nestedKey: 1 }],
})

const fetchDataAfterHydration = vi.fn<Array<unknown>, unknown>()
const fetchDataAfterHydration =
vi.fn<(...args: Array<unknown>) => unknown>()
await hydrationClient.prefetchQuery({
queryKey: ['string'],
queryFn: fetchDataAfterHydration,
Expand Down Expand Up @@ -280,7 +281,8 @@ describe('dehydration and rehydration', () => {
})?.state.data,
).toBe('string')

const fetchDataAfterHydration = vi.fn<Array<unknown>, unknown>()
const fetchDataAfterHydration =
vi.fn<(...args: Array<unknown>) => unknown>()
await hydrationClient.prefetchQuery({
queryKey: ['string', { key: ['string'], key2: 0 }],
queryFn: fetchDataAfterHydration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ describe('InfiniteQueryObserver', () => {
test('should stop refetching if undefined is returned from getNextPageParam', async () => {
const key = queryKey()
let next: number | undefined = 2
const queryFn = vi.fn<any, any>(({ pageParam }) => String(pageParam))
const queryFn = vi.fn<(...args: Array<any>) => any>(({ pageParam }) =>
String(pageParam),
)
const observer = new InfiniteQueryObserver(queryClient, {
queryKey: key,
queryFn,
Expand All @@ -133,7 +135,9 @@ describe('InfiniteQueryObserver', () => {
test('should stop refetching if null is returned from getNextPageParam', async () => {
const key = queryKey()
let next: number | null = 2
const queryFn = vi.fn<any, any>(({ pageParam }) => String(pageParam))
const queryFn = vi.fn<(...args: Array<any>) => any>(({ pageParam }) =>
String(pageParam),
)
const observer = new InfiniteQueryObserver(queryClient, {
queryKey: key,
queryFn,
Expand Down
26 changes: 15 additions & 11 deletions packages/query-core/src/__tests__/query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,9 @@ describe('query', () => {

const queryFn = vi
.fn<
[QueryFunctionContext<ReturnType<typeof queryKey>>],
Promise<'data'>
(
context: QueryFunctionContext<ReturnType<typeof queryKey>>,
) => Promise<'data'>
>()
.mockResolvedValue('data')

Expand Down Expand Up @@ -280,10 +281,12 @@ describe('query', () => {
test('should provide an AbortSignal to the queryFn that provides info about the cancellation state', async () => {
const key = queryKey()

const queryFn = vi.fn<
[QueryFunctionContext<ReturnType<typeof queryKey>>],
Promise<unknown>
>()
const queryFn =
vi.fn<
(
context: QueryFunctionContext<ReturnType<typeof queryKey>>,
) => Promise<unknown>
>()
const onAbort = vi.fn()
const abortListener = vi.fn()
let error
Expand Down Expand Up @@ -330,7 +333,7 @@ describe('query', () => {
test('should not continue if explicitly cancelled', async () => {
const key = queryKey()

const queryFn = vi.fn<Array<unknown>, unknown>()
const queryFn = vi.fn<(...args: Array<unknown>) => unknown>()

queryFn.mockImplementation(async () => {
await sleep(10)
Expand Down Expand Up @@ -362,7 +365,7 @@ describe('query', () => {
test('should not error if reset while pending', async () => {
const key = queryKey()

const queryFn = vi.fn<Array<unknown>, unknown>()
const queryFn = vi.fn<(...args: Array<unknown>) => unknown>()

queryFn.mockImplementation(async () => {
await sleep(10)
Expand All @@ -389,7 +392,7 @@ describe('query', () => {
test('should be able to refetch a cancelled query', async () => {
const key = queryKey()

const queryFn = vi.fn<Array<unknown>, unknown>()
const queryFn = vi.fn<(...args: Array<unknown>) => unknown>()

queryFn.mockImplementation(async () => {
await sleep(50)
Expand Down Expand Up @@ -895,8 +898,9 @@ describe('query', () => {

const queryFn = vi
.fn<
[QueryFunctionContext<ReturnType<typeof queryKey>>],
Promise<unknown>
(
context: QueryFunctionContext<ReturnType<typeof queryKey>>,
) => Promise<unknown>
>()
.mockImplementation(({ signal }) => {
return new Promise((resolve, reject) => {
Expand Down
Loading

0 comments on commit bb08ee8

Please sign in to comment.