Skip to content

Commit

Permalink
[useResizeObserver] Update example code
Browse files Browse the repository at this point in the history
Reference: #7518 (comment)
Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee committed Feb 8, 2024
1 parent defc836 commit 0b5b93a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
4 changes: 0 additions & 4 deletions changelogs/upcoming/7518.md

This file was deleted.

8 changes: 4 additions & 4 deletions src-docs/src/views/resize_observer/resize_observer_hook.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState } from 'react';
import React, { useState } from 'react';

import {
EuiButton,
Expand All @@ -24,8 +24,8 @@ export const ResizeObserverHookExample = () => {
setItems((items) => [...items, `Item ${items.length + 1}`]);
};

const resizeRef = useRef();
const dimensions = useResizeObserver(resizeRef.current);
const [resizeRef, setResizeRef] = useState();
const dimensions = useResizeObserver(resizeRef);

return (
<div>
Expand Down Expand Up @@ -56,7 +56,7 @@ export const ResizeObserverHookExample = () => {

<EuiSpacer />

<div className="eui-displayInlineBlock" ref={resizeRef}>
<div className="eui-displayInlineBlock" ref={setResizeRef}>
<EuiPanel className="eui-displayInlineBlock" paddingSize={paddingSize}>
<ul>
{items.map((item) => (
Expand Down
7 changes: 2 additions & 5 deletions src/components/observer/resize_observer/resize_observer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ export const useResizeObserver = (
container: Element | null,
dimension?: 'width' | 'height'
) => {
const [size, _setSize] = useState<{
width: undefined | number;
height: undefined | number;
}>({ width: undefined, height: undefined });
const [size, _setSize] = useState({ width: 0, height: 0 });

// _currentDimensions and _setSize are used to only store the
// new state (and trigger a re-render) when the new dimensions actually differ
Expand Down Expand Up @@ -122,5 +119,5 @@ export const useResizeObserver = (
}
}, [container, setSize]);

return { width: size.width ?? 0, height: size.height ?? 0 };
return { width: size.width, height: size.height };
};

0 comments on commit 0b5b93a

Please sign in to comment.