From cfd485790dc99747630f809e317b65e05dd7bb6b Mon Sep 17 00:00:00 2001 From: Brian Vaughn Date: Tue, 30 Dec 2025 10:33:13 -0500 Subject: [PATCH] Separate renderProp and ChildComponent props --- CHANGELOG.md | 4 ++ README.md | 39 ++++++----- integrations/vite/src/main.tsx | 2 + integrations/vite/src/routes/Home.tsx | 58 +++++++++++++--- .../vite/src/routes/ScrollingDomState.tsx | 30 ++++++++ integrations/vite/tests/dom-state.spec.tsx | 19 ++++++ integrations/vite/tests/render.spec.tsx | 62 +++++++++++++---- lib/components/AutoSizer.test.tsx | 43 ++++++------ lib/components/AutoSizer.tsx | 30 +++++--- lib/components/types.ts | 68 ++++++++++++++----- lib/index.ts | 5 +- package.json | 2 +- pnpm-lock.yaml | 10 +-- public/generated/docs/AutoSizer.json | 64 +++++++++++++---- public/generated/examples/ChildComponent.json | 2 +- public/generated/examples/RenderProp.json | 2 +- src/App.tsx | 8 +-- src/routes.ts | 4 +- src/routes/BasicUsageRoute.tsx | 16 ----- src/routes/ChildComponentRoute.tsx | 20 ++++++ src/routes/RenderPropRoute.tsx | 4 +- src/routes/examples/ChildComponent.tsx | 6 +- src/routes/examples/RenderProp.tsx | 10 +-- 23 files changed, 368 insertions(+), 140 deletions(-) create mode 100644 integrations/vite/src/routes/ScrollingDomState.tsx create mode 100644 integrations/vite/tests/dom-state.spec.tsx delete mode 100644 src/routes/BasicUsageRoute.tsx create mode 100644 src/routes/ChildComponentRoute.tsx diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c3f4fe..972e236 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +# 2.0.1 + +- [#104](https://github.com/bvaughn/react-virtualized-auto-sizer/pull/104): Separate `renderProp` and `ChildComponent` props. + # 2.0.0 Version 2 simplifies the API and improves TypeScript support. diff --git a/README.md b/README.md index d0abeae..7a7c378 100644 --- a/README.md +++ b/README.md @@ -24,24 +24,7 @@ Measures the available width and height of its parent `HTMLElement` and passes t #### Required props - - - - - - - - - - - - - - -
NameDescription
Child

Child component to be passed the available width and height values as props.

-

ℹ️ Width and height are undefined during the during the initial render (including server-rendering).

-
- +None #### Optional props @@ -102,6 +85,26 @@ in browsers/environments that do not support the ResizeObserver API tagName

Optional HTML tag name for root HTMLElement; defaults to "div".

+ + + + Child +

Child component to be passed the available width and height values as props. +@deprecated Use the ChildComponent or renderProp props instead.

+ + + + ChildComponent +

Child component to be passed the available width and height values as props.

+

ℹ️ Use renderProp instead if you need access to local state.

+

⚠️ Width and height are undefined during the during the initial render (including server-rendering).

+ + + + renderProp +

Render prop to be passed the available width and height values as props.

+

ℹ️ Use ChildComponent instead for better memoization if you do not need access to local state.

+

⚠️ Width and height are undefined during the during the initial render (including server-rendering).

diff --git a/integrations/vite/src/main.tsx b/integrations/vite/src/main.tsx index 30740b3..a5e9ad0 100644 --- a/integrations/vite/src/main.tsx +++ b/integrations/vite/src/main.tsx @@ -3,12 +3,14 @@ import { createRoot } from "react-dom/client"; import { BrowserRouter, Route, Routes } from "react-router"; import "./index.css"; import { Home } from "./routes/Home"; +import { ScrollingDomState } from "./routes/ScrollingDomState"; createRoot(document.getElementById("root")!).render( } /> + } /> diff --git a/integrations/vite/src/routes/Home.tsx b/integrations/vite/src/routes/Home.tsx index 6b5b509..bd445af 100644 --- a/integrations/vite/src/routes/Home.tsx +++ b/integrations/vite/src/routes/Home.tsx @@ -3,22 +3,25 @@ import { useLayoutEffect, useMemo, useState, - type FunctionComponent + type FunctionComponent, + type ReactNode } from "react"; import { createPortal } from "react-dom"; +import { useSearchParams } from "react-router"; import { AutoSizer, type AutoSizerBox, type Size } from "react-virtualized-auto-sizer"; import { Children, type SizeProps } from "../components/Children"; -import { useSearchParams } from "react-router"; + +export type ChildProp = "Child" | "ChildComponent" | "renderProp"; export function Home() { const [params] = useSearchParams(); const box = (params.get("box") || undefined) as AutoSizerBox | undefined; + const childProp = (params.get("childProp") || "ChildComponent") as ChildProp; const style = params.get("style") || ""; - console.log("Home:", JSON.stringify({ box, style }, null, 2)); const [container] = useState(() => { const div = document.createElement("div"); @@ -40,7 +43,7 @@ export function Home() { const [commits, setCommits] = useState([]); const [onResizeCalls, setOnResizeCalls] = useState([]); - const Child = useMemo>( + const ChildComponent = useMemo>( () => ({ height, width }) => ( + ); + break; + } + case "ChildComponent": { + autoSizer = ( + + ); + break; + } + case "renderProp": { + autoSizer = ( + ( + + )} + /> + ); + break; + } + } + return (
-