Skip to content

Instantly share code, notes, and snippets.

@Stanley-Yao
Last active August 15, 2021 04:33
Show Gist options
  • Save Stanley-Yao/fe59b183907e836791896cb5ce37ee10 to your computer and use it in GitHub Desktop.
Save Stanley-Yao/fe59b183907e836791896cb5ce37ee10 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState, useCallback } from "react";
import { useResizeDetector } from "react-resize-detector";
const AppGamma = () => {
const [widthA, setWidthA] = useState<number>(800);
const onResize = useCallback(() => {
// on resize logic
}, []);
const { width, ref } = useResizeDetector({
onResize,
refreshMode: "debounce"
});
useEffect(() => {
//do something with the width, remember to use debounce
console.log(width);
}, [width]);
return (
<>
<h3> App Gamma </h3>
<div>
<button onClick={() => setWidthA(widthA + 100)}> ++ width </button>
<button onClick={() => setWidthA(widthA - 100)}> -- width </button>
</div>
<div className="container-gamma">
<div className="child-app-A" style={{ width: widthA }}>
Widget A
</div>
<div className="child-app-B" ref={ref}>
Widget B width: <div>{width} px</div>
</div>
</div>
</>
);
};
export default AppGamma;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment