Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React Hooks API proposal #166

Open
ctrlplusb opened this issue May 1, 2019 · 4 comments
Open

React Hooks API proposal #166

ctrlplusb opened this issue May 1, 2019 · 4 comments

Comments

@ctrlplusb
Copy link
Owner

ctrlplusb commented May 1, 2019

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. 👍

@paynecodes
Copy link

paynecodes commented Apr 28, 2020

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

@ctrlplusb
Copy link
Owner Author

I really love that thanks for the share 💜

@kachkaev
Copy link

kachkaev commented May 2, 2020

Also

@alvaro1728
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants