react-sizeme icon indicating copy to clipboard operation
react-sizeme copied to clipboard

React Hooks API proposal

Open ctrlplusb opened this issue 6 years ago • 4 comments

Thinking of something like the following:

import { useSizeMe } from 'react-sizeme/hooks'; // 👈 note the "/hooks"

function MyComponent() {
  const [sized, width, height] = useSizeMe(
    ({ width, height }) => <div>My dimensions are {width} x {height}</div>,
    { monitorHeight: true }
  );
  return (
    <div>
      {sized}
      <p>The above element's dimensions are {width} x {height}</p>
    </div>
  );
}

Things to note:

  • You can access the dimensions both inside and outside your hook
  • The sizeme options are provided as second argument to hook, and are optional
  • The import of the hook is via "react-sizeme/hooks". This is so we can introducing a breaking change, as hooks require React v16.8+. The old API will be unaffected. You simply need to make sure you have the correct version of React if you plan to use the hooks API.

Demo: https://codesandbox.io/s/13m9vq68pl

Would appreciate some feedback. You can easily copy the hook from the demo and use within your codebase to play around. 👍

ctrlplusb avatar May 01 '19 15:05 ctrlplusb

What are your thoughts on the API over here https://github.com/Swizec/useDimensions?

jarrodpayne avatar Apr 28 '20 21:04 jarrodpayne

I really love that thanks for the share 💜

ctrlplusb avatar Apr 29 '20 10:04 ctrlplusb

Also

kachkaev avatar May 02 '20 13:05 kachkaev

Another useful hook: https://usehooks.com/useWindowSize/

alvaro1728 avatar Apr 14 '21 20:04 alvaro1728