0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-18 19:33:22 +00:00
Commit graph

5,624 commits

Author SHA1 Message Date
Tanner Linsley
2de852896b chore: Reorganize code to core + react 2020-06-26 10:03:45 -06:00
0xflotus
bcecb5476e
(docs): fixed small errors (#636) 2020-06-25 13:25:10 -06:00
Tanner Linsley
ce25ffa5da Update README.md 2020-06-25 10:45:30 -06:00
Tanner Linsley
d7048d1c35 feat: add initialStale option to queries v2.4.0
By default `initialData` is not considered stale, but sometimes you may want it to be, for instance, if your initial data is only a partial subset of an object and you know you will need to refetch the full version immediately after mounting. For this, you can use the `initialStale: true` options.

By setting `initialStale` to `true`, the `initialData` will be considered `stale` and will cause a refetch when the query mounts for the first time.

```js
function Todos() {
  const queryInfo = useQuery('todos', () => fetch('/todos'), {
    initialData: todoListPreview,
    initialStale: true,
  })
}
```
2020-06-25 10:38:50 -06:00
Tanner Linsley
8ec83f43fa Merge branch 'master' of https://github.com/tannerlinsley/react-query v2.3.0 2020-06-25 09:47:21 -06:00
Tanner Linsley
9a734f617d feat: add refetchInactive option to invalidateQueries
Normally, inactive queries do not refetch when they are invalidated, but you can now use the `refetchInactive: true` option to force them to refetch.
2020-06-25 09:47:19 -06:00
Kent C. Dodds
4712c0d4d7
fix: prefetchQuery args check (#631) v2.2.3 2020-06-24 23:17:04 -06:00
Tanner Linsley
0054f7ffff fix(prefetchQuery): add prefetchQuery(key, options) overload v2.2.2 2020-06-24 19:35:07 -06:00
Kurt Furbush
340e4adefb
Typo (#630) 2020-06-24 19:28:23 -06:00
Tanner Linsley
ab0fbeaf3f Update README.md 2020-06-24 18:41:06 -06:00
Tanner Linsley
660ef1e73c Update package.json 2020-06-24 18:34:26 -06:00
Tanner Linsley
a68dc28727 fix: queryKeySerializer now in the right spot of the config v2.2.1 2020-06-24 17:17:43 -06:00
Tanner Linsley
ef5becbb15 Update README.md 2020-06-24 16:51:16 -06:00
Tanner Linsley
2a31f8d005 feat: add default quernFn option v2.2.0
This feature allows you to set a default query function in the config and use it for your whole app, similar to how early SWR user's were just passing a url. Now you can do the same and just do `useQuery('/todos')` :)
2020-06-24 16:49:11 -06:00
Tanner Linsley
0bf38da923 chore: revort index in basic example 2020-06-24 16:41:48 -06:00
Tanner Linsley
c9afbc9ddf fix: call onSuccess for all query instances 2020-06-24 16:40:44 -06:00
Tanner Linsley
a05cb5073f fix: call onSuccess for all query instances on
Fixes #629
2020-06-24 16:07:14 -06:00
Tanner Linsley
a5148284ff Merge branch 'master' of https://github.com/tannerlinsley/react-query 2020-06-24 14:36:00 -06:00
Tanner Linsley
e770a78140 docs: remove experiemental custom query key serializers 2020-06-24 14:35:57 -06:00
Kent C. Dodds
1e1e0f67e6
fix: don't schedule garbage collection unnecessarily (#627)
This doesn't actually fix the issue I'm looking into, but I did notice that this timeout is scheduled even when the query is cleared and I *think* it shouldn't be. If we do need to garbage collect something, that should happen in `clear` I think.
2020-06-24 12:19:12 -06:00
David Meade
6922cda1f1
docs(README) typo fix (#623) 2020-06-24 12:14:20 -06:00
Tanner Linsley
0147a8ee1f Update README.md 2020-06-24 12:11:02 -06:00
Tanner Linsley
fd280eca7e fix: query.state.isFetching defaults to the loading status, despite lack of data v2.1.1
Fixes #624
2020-06-24 11:58:59 -06:00
Tanner Linsley
4c741f7499 feat: added queryCache.resetErrorBoundaries v2.1.0
Info can be found in the docs under the `Resetting Error Boundaries` section
2020-06-24 11:45:13 -06:00
Tanner Linsley
8519dd4d6d Merge branch 'master' of https://github.com/tannerlinsley/react-query v2.0.4 2020-06-24 10:39:23 -06:00
Tanner Linsley
c3e42c44af fix(refetchOnWindowFocus): stale queries are no longer refetched 2020-06-24 10:39:15 -06:00
mrroll
bc40a0d5b0
docs: Typo fix (#612) 2020-06-24 10:29:27 -06:00
mick mcmahon
8a401304ca
Update README.md (#613)
Typo - Yay!!!! I contributed!!
2020-06-24 10:29:15 -06:00
Dávid Kizivat
7c32aa4b51
chore(types): Add missing params for mutation types (#617)
* fix(types): Add variable to mutate function

The MutateFunction didn't accept variables when not undifined as expected

* fix(types): Add onMutateValue param to onError mutate option
2020-06-24 10:29:00 -06:00
Tobias Büschel
f17aebad94
docs: correct broken hyperlinks in README.md (#619) 2020-06-24 10:28:25 -06:00
Tanner Linsley
f844de8487 chore: update examples 2020-06-24 10:22:17 -06:00
Tanner Linsley
46b02aae26 Merge branch 'master' of https://github.com/tannerlinsley/react-query 2020-06-24 10:19:23 -06:00
Tanner Linsley
1265818213 Update README.md
Fixes #620
2020-06-24 10:19:13 -06:00
Zade Kaakarni
7d131e6ee3
Update Table of Contents (#607)
* Remove "Manual Querying" section link

* Update TOC

Co-authored-by: Zade Kaakarni <zade.kaakarni@freightverify.com>
2020-06-23 15:58:25 -06:00
Cory House
60c4930d4f
docs: Typo fix (#605) 2020-06-23 12:39:53 -06:00
Augusto
c3424bb481
docs: fix typo (#603) 2020-06-23 09:46:59 -06:00
Tanner Linsley
ebbf1a849b
fix: remove unused wasPrefetched v2.0.3 2020-06-22 20:01:21 -06:00
Tanner Linsley
5ce7b1e8d1 test: Add more query key tests 2020-06-22 17:56:31 -06:00
Tanner Linsley
9c618f37d5 fix(retries): cancel retry timer when query is cleared v2.0.2 2020-06-22 17:43:14 -06:00
Tanner Linsley
8eec19ac65 fix(types): temporarily disable types for 2.0 v2.0.1 2020-06-22 16:00:22 -06:00
Tanner Linsley
84d1a19b02 v2 v2.0.0
(#585)

- Better query invalidation & refetching, less over-fetching
- Simplified dependent query syntax and new `idle` query state
- Multi-instance polling and interval support
- New query status booleans
- Bidirectional Infinite Queries
- Improved mutation lifecycle
- Better test cleanup utils
- Prep for future SSR enhancements

- The booleans `isSuccess`, `isError` `isLoading` and a new one called `isIdle` have been added to the queryInfo object returned by `useQuery` (and friends) and `useMutation`. These are derived safely from the `queryInfo.status` and are guaranteed to not overlap. In most situations, they are easier to use, less typo-prone than status strings and also more terse for determining what to render based on the status of a query.

  ```js
  const queryInfo = useQuery(queryKey, fn)

  return queryInfo.isLoading ? (
    'Loading...'
  ) : queryInfo.isError ? (
    queryInfo.error.message
  ) : (
    <div>{queryInfo.data}</div>
  )
  ```

- `queryCaches` is now exported, which allows you to clean up all query caches that were created. We do this in our own tests when multiple caches are used for testing and to be thorough.

  ```js
  // test.js
  import { queryCaches } from 'react-query'

  afterEach(() => {
    queryCaches.forEach((queryCache) => queryCache.clear())
  })
  ```

- `fetchMore` now supports an optional `previous` option, which will determine if the data you are fetching is should be prepended instead of appended to your infinite list. eg, `fetchMore(nextPageVars, { previous: true })`

  ```js
  const { fetchMore } = useInfiniteQuery(queryKey, fn)

  return (
    <button
      onClick={() => fetchMore(previousPageVariables, { previous: true })}
    />
  )
  ```

- `refetchInterval` can now be changed on the fly. Check out the auto-refetching example to see it in action!
- `invalidateQueries` (previously `refetchQueries`) now has an option called `refetchActive` that when set to `false` will **not** refetch matching queries that are active on the page.
- `makeQueryCache` now accepts an optional configuration object. The `defaultConfig` object is used to override the default query configuration config to use inside of this cache. The `frozen` option if set to `true` will simulate React Query being run on the server, where no queries are cached for privacy and safety concerns. This is the default when using React Query on the server and is optional. You can also set it to `true` on the server and have it work as it would on the client. More information on this coming soon!

BREAKING CHANGE:

- Do you use falsy query keys for dependent queries?
  - The new way to do dependent queries to instead use the `enabled` config flag. You should move your conditions in your key to the `enabled` option
    - Before
    ```js
    useQuery(ready && queryKey, queryFn)
    ```
    - After
    ```js
    useQuery(queryKey, queryFn, { enabled: ready })
    ```
- Do you use functions as queryKeys for dependent queries?
  - If you use functions as query keys that can potentially throw, you should migrate their logic to the `enabled` config option and use optional chaining to cast them to a boolean
    - Before
    ```js
    useQuery(() => ['user', user.id], queryFn)
    ```
    - After
    ```js
    useQuery(['user', user?.id], queryFn, { enabled: user?.id })
    ```
- Do you expect dependent queries to start in the `success` state?

  - Dependent queries now start in a new `idle` state. You should account for this new state where possible. Most rendering logic should still work if you are checking first for the `loading` and `error` states first, then falling back to the "success" state, but still... it's a good idea to do this

    - Before

    ```js
    const { status, data } = useQuery(key, fn)

    return status === 'loading'
      ? 'Loading...'
      : status === 'error'
      ? error.message
      : data
      ? 'The Data'
      : 'Not Ready'
    ```

    - After

    ```js
    const { status } = useQuery(key, fn

    return status === 'idle' ? 'Not Ready' : status === 'loading'
      ? 'Loading...'
      : status === 'error'
      ? error.message
      : 'The Data'
    ```

- Do you use `queryCache.refetchQueries`?
  - `refetchQueries` has been renamed to `invalidateQueries`. You will need make this rename change for your app to continue working propertly. The name change comes due to some differences in what the function does.
    - Before, any queries that matched the `queryKey` used in `refetchQueries(queryKey)` and were also stale, would be refetched... \*\*even queries that were inactive and not rendered on the screen. This resulted in quite a few queries being refetched regardless of their immediate necessity.
    - Now, with `invalidateQueries`, only queries that are actively rendered will be refetched, while any other matching queries will forcefully be marked as stale.
    - This probably won't affect much performance, but should help reduce overfetching out of the box.
- Did you expect `queryCache.refetchQueries` to only refetch stale queries?
  - The new `invalidateQueries` method will **always refetch matching queries that are active**. All other matched queries that are not active will be immediately marked as stale.
- Do you call `queryCache.refetchQueries` with the `force` option?
  - Before, the `force` option was a way to force queries that were not stale to refetch.
  - Now, he new `invalidateQueries` method will **always refetch matching queries that are active** and all other matched queries that are not active will be immediately marked as stale.
- Do you use a global configuration object to configure React Query?
  - Before, the global configuration object was flat:
    ```js
    const globalConfig = {
      suspense,
      useErrorBoundary,
      throwOnError,
      refetchAllOnWindowFocus,
      queryKeySerializerFn,
      onMutate,
      onSuccess,
      onError,
      onSettled,
      retry,
      retryDelay,
      staleTime,
      cacheTime,
      refetchInterval,
      queryFnParamsFilter,
      refetchOnMount,
      isDataEqual,
    }
    ```
  - Now, the global configuration object has 3 parts. The `shared` section, which is a base set of options that are inherited into the next 2 sections, `queries` and `mutations`, each corresponding to the functionality they are used for in React Query:
    ```js
    const globalConfig = {
      shared: {
        suspense,
        queryKeySerializerFn,
      },
      queries: {
        ...shared,
        enabled,
        retry,
        retryDelay,
        staleTime,
        cacheTime,
        refetchOnWindowFocus,
        refetchInterval,
        queryFnParamsFilter,
        refetchOnMount,
        isDataEqual,
        onError,
        onSuccess,
        onSettled,
        throwOnError,
        useErrorBoundary,
      },
      mutations: {
        ...shared,
        throwOnError,
        onMutate,
        onError,
        onSuccess,
        onSettled,
        useErrorBoundary,
      },
    }
    ```
- Do you use "optional query variables" eg. `useQuery(queryKey, optionalVariables, queryFn)` or `useQuery({ variables })`?
  - Optional variables have been removed. They were not used by many and also were unnecessary seeing how you can inline them into your query function
  - Before
    ```js
    useQuery('todos', [optional, variables], queryFn)
    ```
  - After
    ```js
    useQuery('todos', (key) => queryFn(key, optional, variables))
    ```
- Do you use the `globalConfig.refetchAllOnWindowFocus` config option?
  - `refetchAllOnWindowFocus` has been renamed to `refetchOnWindowFocus` to match the option in the configuration object for `useQuery` and friends.
- Do you use the `refetch` function returned by `useQuery` and friends?
  - Previously this `refetch` function would not trigger an actual refetch if the query is not stale.
  - Now, calling this `refetch` will always trigger a refetch, regardless if the query is stale or not.
- Do you expect `prefetchQuery` to skip the first render of `useQuery` instances that render after it?
  - Previously, the first `useQuery` call after a `prefetchQuery` would be skipped all the time.
  - Now, the `staleTime` of a `prefetchQuery` instance is honored. So, if you call `prefetchQuery(key, fn, { staletime: 5000 })`, and then `useQuery(key, fn)` is rendered within those initial 5000 milliseconds, the query will **not refetch in the background, since it is not stale yet**. Likewise, if the stale time has been reached by the time `useQuery(key, fn)` renders, **it will refetch in the background**, since it is stale when `useQuery` mounts/renders.
- Do you use `prefetchQueries`' `throwOnError` or `force` options?
  - `prefetchQuery`'s `throwOnError` and `force` options are now located in a fourth argument, after the query config.
  - Before
    ```js
    prefetchQuery(key, fn, { ...queryConfig, throwOnError: true, force: true })
    ```
  - After
    ```js
    prefetchQuery(key, fn, queryConfig, { throwOnError: true, force: true })
    ```
- Do you call `mutate()` with additional side-effect callbacks? eg. `mutate(vars, { onError, onSuccess, onSettled })`

  - There are no code changes here, however previously, `mutate()`-level side effects would run _before_ side-effects defined in `useMutation`. That order has been reversed to make more sense.
  - Now, the side-effect callbacks in `useMutation` will be fired before their `mutate`-level counterparts.
  - Before

    ```js
    const mutate = useMutation(fn, {
      onSuccess: () => console.log('I will run second'),
    })

    mutate(vars, { onSuccess: () => console.log('I will run first') })
    ```

  - After

    ```js
    const mutate = useMutation(fn, {
      onSuccess: () => console.log('I will run first'),
    })

    mutate(vars, { onSuccess: () => console.log('I will run second') })
    ```

- Do you use `setQueryData` to update multiple queries with a single query key? eg. `setQueryData('todos', newData)` and expect queryKeys `['todos', 1]` and `['todos', 2]` to both get updated?
  - `setQueryData` no longer allows updating multiple queries at once (via prefix matching). If you need to update multiple queries with the same data, you can use the `queryCache.getQueries()` function to match all of the queries you want, then loop over them and use their `query.setData` function to set all of them to the same value.
Co-authored-by: Fredrik Höglund <fredrik.hoglund@gmail.com>
Co-authored-by: Pepijn Senders <PepijnSenders@users.noreply.github.com>
Co-authored-by: Jack <jack.m.ellis@hotmail.co.uk>
Co-authored-by: Jack Ellis <jack.ellis@godaddy.com>
Co-authored-by: Jake Ginnivan <jake@ginnivan.net>
2020-06-22 15:47:18 -06:00
Pepijn Senders
4a37d1b3c4
chore(queryCache): add config parameter to setQueryData call (#587) 2020-06-22 12:06:36 -06:00
Fagner Schwalm
1fc7ed08d1
fix(types): add mutateOptions on MutationFunction (#594) v1.5.9 2020-06-19 14:43:50 -06:00
Tanner Linsley
12665c34e0
Update README.md 2020-06-17 19:39:29 -06:00
Tanner Linsley
6b65427794
Update test-and-publish.yml 2020-06-16 20:11:44 -06:00
Jason O'Neill
06feac8a2d
chore: upgrade dependencies (#583) v1.5.8
* chore: upgrade dependencies

* add workaround for suspense test

* remove size workflow
2020-06-14 19:15:18 -06:00
Jason O'Neill
87008db3a0
fix(types): enable more types for first QueryKey (#577)
* fix(types): enable more types for first QueryKey

* fix(types): enable more types for first QueryKey

* test: update query key test case
2020-06-13 21:13:12 -06:00
Jason O'Neill
21b9b01e5d
fix(types): change return type of removeQueries (#582) 2020-06-13 20:45:49 -06:00
Tanner Linsley
e6d70bd85d Update issue templates 2020-06-11 15:14:06 -06:00
Tanner Linsley
a130369c02 Create config.yml 2020-06-11 15:13:06 -06:00