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
-
-
-
-
-
Name
-
Description
-
-
-
-
-
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 (
// Render prop can access width and height params,
\n
// as well as the parent component props and state
\n
}
\n
}/>
\n
)
\n
}
"
}
\ No newline at end of file
diff --git a/src/App.tsx b/src/App.tsx
index e21e8ea..dbb8552 100644
--- a/src/App.tsx
+++ b/src/App.tsx
@@ -16,8 +16,8 @@ export default function App() {
This package measures the available width and height of an{" "}
HTMLElement and passes those values as props to a{" "}
- Child component. Refer to the{" "}
- examples or{" "}
+ ChildComponent or renderProp. Refer to the{" "}
+ examples or{" "}
props pages for more
information.
- AutoSizer measures the available width and height of its
- parent HTMLElement and passes them as props to a{" "}
- Child component.
-
-
-
- );
-}
diff --git a/src/routes/ChildComponentRoute.tsx b/src/routes/ChildComponentRoute.tsx
new file mode 100644
index 0000000..769b9e3
--- /dev/null
+++ b/src/routes/ChildComponentRoute.tsx
@@ -0,0 +1,20 @@
+import { Box, Callout, Code, Header } from "react-lib-tools";
+import { html } from "../../public/generated/examples/ChildComponent.json";
+
+export default function ChildComponentRoute() {
+ return (
+
+
+
+ AutoSizer measures the available width and height of its
+ parent HTMLElement and passes them as props to a child
+ component.
+
+
+
+ The child component is auto-memoized to avoid re-rendering until the
+ size has changed.
+
+
+ );
+}
diff --git a/src/routes/RenderPropRoute.tsx b/src/routes/RenderPropRoute.tsx
index 6edcc52..bd39c39 100644
--- a/src/routes/RenderPropRoute.tsx
+++ b/src/routes/RenderPropRoute.tsx
@@ -4,9 +4,9 @@ import { html } from "../../public/generated/examples/RenderProp.json";
export default function RenderPropRoute() {
return (
-
+
- The Child component can also be a{" "}
+ The child of an AutoSizer can also be a{" "}
render prop
diff --git a/src/routes/examples/ChildComponent.tsx b/src/routes/examples/ChildComponent.tsx
index 9ef75f7..923c40f 100644
--- a/src/routes/examples/ChildComponent.tsx
+++ b/src/routes/examples/ChildComponent.tsx
@@ -2,16 +2,16 @@ import { AutoSizer, type AutoSizerChildProps } from "react-virtualized-auto-size
function ExampleComponent() {
return (
-
+
)
}
// Height and width will be undefined for the initial render (or when server rendering)
// You can either set default values (as shown below) or render some sort of placeholder
-function Child({ height = 600, width = 600 }: AutoSizerChildProps) {
+function ChildComponent({ height = 600, width = 600 }: AutoSizerChildProps) {
return
{width} x {height} pixels
;
}
//
-export { Child, ExampleComponent };
+export { ChildComponent, ExampleComponent };
diff --git a/src/routes/examples/RenderProp.tsx b/src/routes/examples/RenderProp.tsx
index b4a5e06..873589a 100644
--- a/src/routes/examples/RenderProp.tsx
+++ b/src/routes/examples/RenderProp.tsx
@@ -5,14 +5,14 @@ import { useState } from "react";
import { AutoSizer } from "react-virtualized-auto-sizer";
function ExampleComponent(props: ExampleComponentProps) {
- const [state, setState] = useState();
+ const [state, setState] = useState("...");
return (
- {
// Render prop can access width and height params,
// as well as the parent component props and state
- setState; // hidden
+ setState(""); // hidden
return `${props}${state}${height}${width}`; // hidden
}
} />
@@ -21,6 +21,8 @@ function ExampleComponent(props: ExampleComponentProps) {
//
-type ExampleComponentProps = {};
+type ExampleComponentProps = {
+ foo: string;
+};
export { ExampleComponent };