From fcc3e0a1e6dbe1cfacb8aecc6314b7b8377bc49b Mon Sep 17 00:00:00 2001 From: talksik Date: Tue, 23 Jul 2024 16:51:20 -0700 Subject: [PATCH] adding example --- package.json | 2 ++ src/App.tsx | 61 ++++++++++++++++++++++++++++++++++++++-------------- yarn.lock | 19 ++++++++++++++++ 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index f7d332d..11b243a 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { + "@tanstack/react-query": "^5.51.11", "@testing-library/jest-dom": "^5.14.1", "@testing-library/react": "^13.0.0", "@testing-library/user-event": "^13.2.1", @@ -12,6 +13,7 @@ "@types/react-dom": "^18.0.0", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-error-boundary": "^4.0.13", "react-scripts": "5.0.1", "typescript": "^4.4.2", "web-vitals": "^2.1.0" diff --git a/src/App.tsx b/src/App.tsx index a53698a..ddd94d5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,24 +1,53 @@ -import React from 'react'; -import logo from './logo.svg'; import './App.css'; +import { QueryClient, QueryClientProvider, useQueryErrorResetBoundary, useSuspenseQuery } from '@tanstack/react-query'; +import { ErrorBoundary } from 'react-error-boundary' + +// Create a client +const queryClient = new QueryClient() function App() { + const { reset } = useQueryErrorResetBoundary() + + return ( + + ( +
+ There was an error! + +
+ )} + > + +
+
+ ) +} + +// mock the idea of protected component/requiring authentication +let count = 0; + +function Todos() { + const { data } = useSuspenseQuery({ + queryKey: ['todos'], + queryFn: async () => { + console.log('fetching data'); + console.log(count); + if (count === 0) { + count++; + throw new Error('This is an error'); + } + const response = await fetch('https://jsonplaceholder.typicode.com/todos'); + const data = await response.json() as any[]; + return data; + }, + retry: false, + }); + return (
-
- logo -

- Edit src/App.tsx and save to reload. -

- - Learn React - -
+ {`got data ${data.length}`}
); } diff --git a/yarn.lock b/yarn.lock index 36a11f6..012552c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1865,6 +1865,18 @@ "@svgr/plugin-svgo" "^5.5.0" loader-utils "^2.0.0" +"@tanstack/query-core@5.51.9": + version "5.51.9" + resolved "https://registry.yarnpkg.com/@tanstack/query-core/-/query-core-5.51.9.tgz#eb2e02c715068d5664680b773e39eb44db6b48d8" + integrity sha512-HsAwaY5J19MD18ykZDS3aVVh+bAt0i7m6uQlFC2b77DLV9djo+xEN7MWQAQQTR8IM+7r/zbozTQ7P0xr0bHuew== + +"@tanstack/react-query@^5.51.11": + version "5.51.11" + resolved "https://registry.yarnpkg.com/@tanstack/react-query/-/react-query-5.51.11.tgz#8ab2cf6a4baa7a99b5682addf31328525f43242c" + integrity sha512-4Kq2x0XpDlpvSnaLG+8pHNH60zEc3mBvb3B2tOMDjcPCi/o+Du3p/9qpPLwJOTliVxxPJAP27fuIhLrsRdCr7A== + dependencies: + "@tanstack/query-core" "5.51.9" + "@testing-library/dom@^8.5.0": version "8.20.1" resolved "https://registry.yarnpkg.com/@testing-library/dom/-/dom-8.20.1.tgz#2e52a32e46fc88369eef7eef634ac2a192decd9f" @@ -7935,6 +7947,13 @@ react-dom@^18.3.1: loose-envify "^1.1.0" scheduler "^0.23.2" +react-error-boundary@^4.0.13: + version "4.0.13" + resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-4.0.13.tgz#80386b7b27b1131c5fbb7368b8c0d983354c7947" + integrity sha512-b6PwbdSv8XeOSYvjt8LpgpKrZ0yGdtZokYwkwV2wlcZbxgopHX/hgPl5VgpnoVOWd868n1hktM8Qm4b+02MiLQ== + dependencies: + "@babel/runtime" "^7.12.5" + react-error-overlay@^6.0.11: version "6.0.11" resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.11.tgz#92835de5841c5cf08ba00ddd2d677b6d17ff9adb"