ctrlplusb / react-sizeme

Make your React Components aware of their width and height!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

React Hooks API proposal

ctrlplusb opened this issue Β· 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. πŸ‘

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

I really love that thanks for the share πŸ’œ

Also