diff --git a/client/src/pages/Notes/ReactPatterns/ReactNotes/SuspensePattern.jsx b/client/src/pages/Notes/ReactPatterns/ReactNotes/SuspensePattern.jsx new file mode 100644 index 0000000..4b9cb5a --- /dev/null +++ b/client/src/pages/Notes/ReactPatterns/ReactNotes/SuspensePattern.jsx @@ -0,0 +1,157 @@ +import React from "react"; +import CodeBlock from "../../components/CodeBlock"; + +const SuspensePattern = () => { + const examples = [ + { + title: "Basic Suspense with Lazy Loading", + code: `import React, { Suspense, lazy } from "react"; + +const LazyComponent = lazy(() => import("./LazyComponent")); + +function App() { + return ( + Loading...}> + + + ); +} + +export default App;`, + explanation: + "React Suspense lets you display a fallback UI (like a loading spinner) while waiting for a component to load asynchronously. Often used with lazy-loaded components.", + }, + { + title: "Suspense with Data Fetching", + code: `import React, { Suspense } from "react"; + +function DataComponent() { + const data = fetchData(); // Assume fetchData suspends until data is ready + return
{data.title}
; +} + +function App() { + return ( + Fetching data...}> + + + ); +} + +export default App;`, + explanation: + "Suspense can also be used for data fetching when combined with libraries like React Query or Relay, allowing declarative loading states for asynchronous data.", + }, + ]; + + const bestPractices = [ + "Always provide a meaningful fallback UI.", + "Use Suspense for lazy-loaded components and async data fetching.", + "Do not wrap too many components in a single Suspense boundary; isolate boundaries for better UX.", + "Combine Suspense with error boundaries for more resilient applications.", + "Avoid heavy computation in components wrapped by Suspense to prevent blocking the UI.", + ]; + + const commonMistakes = [ + "Using Suspense with non-lazy components or functions that don't suspend.", + "Wrapping the entire app in one Suspense, causing large parts of the UI to show fallback.", + "Not providing a fallback UI, which can cause blank screens during loading.", + "Assuming Suspense catches errors; it only handles loading states.", + ]; + + return ( +
+

+ React{" "} + + Suspense Pattern + +

+ +
+

+ The Suspense Pattern in React helps manage loading + states for lazy-loaded components or asynchronous data. It allows + developers to show fallback content while the component or data is + being prepared. +

+ +
+

+ Tip: Suspense is + declarative — it separates loading logic from your component UI, + making your app cleaner and easier to maintain. +

+
+ + {/* Examples Section */} +
+

+ Examples +

+
+ {examples.map((ex, i) => ( +
+

+ {ex.title} +

+
+ +
+

+ {ex.explanation} +

+
+ ))} +
+
+ + {/* Best Practices */} +
+

+ Best Practices +

+
    + {bestPractices.map((practice, index) => ( +
  • + {practice} +
  • + ))} +
+
+ + {/* Common Mistakes */} +
+

+ Common Mistakes +

+
    + {commonMistakes.map((mistake, index) => ( +
  • + {mistake} +
  • + ))} +
+
+ + {/* Summary */} +
+

+ Summary +

+

+ React Suspense simplifies handling of loading states for components + and data. By using it with lazy-loaded components and async data + fetching, you can create smoother user experiences and maintain + clean component logic. +

+
+
+
+ ); +}; + +export default SuspensePattern; diff --git a/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx b/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx index 6982bea..8c5f0e1 100644 --- a/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx +++ b/client/src/pages/Notes/ReactPatterns/ReactSidebarPage.jsx @@ -34,6 +34,9 @@ import CompoundComp from "./ReactNotes/CompoundComp"; import Props from "./ReactNotes/Props"; import HOC from "./ReactNotes/HOC"; + +import SuspensePattern from "./ReactNotes/SuspensePattern"; + import ErrorBoundaryPattern from "./ReactNotes/ErrorBoundaryPattern"; import ContainerPresenter from "./ReactNotes/ContainerPresenter"; @@ -54,6 +57,7 @@ import RenderOptimization from "./ReactNotes/RenderOptimization"; + const iconMap = { "React Basics": , "React Patterns": , @@ -108,12 +112,16 @@ const topicComponents = { "Props Getters": , + + "What is react" : , + "Slot Pattern": , + "Error Boundary Pattern": , @@ -128,6 +136,9 @@ const topicComponents = { "Render Optimization" : , + + + "Suspense Pattern": // add all other topics here };