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

Does not work with flexbox #29

Closed
ValentinKaisermayer opened this issue Jan 3, 2020 · 3 comments
Closed

Does not work with flexbox #29

ValentinKaisermayer opened this issue Jan 3, 2020 · 3 comments

Comments

@ValentinKaisermayer
Copy link

Minimal example that uses flex boxes that does not work. Link

@ValentinKaisermayer
Copy link
Author

There seems to be something wrong with the variable names. Only

ref, width, height

works.

const App = () => {
  const [open, setOpen] = useState(true);
  const { ref, width, height } = useResizeObserver();
  const { ref2, width2, height2 } = useResizeObserver();
  const { ref3, width3, height3 } = useResizeObserver();

  const renderDiv = () => {
    if (!open) {
      return null;
    }

    return (
      <div
        ref={ref2}
        style={{ flexGrow: "1", backgroundColor: "orange", padding: "1em" }}
      >
        {width2}x{height2}
      </div>
    );
  };

  return (
    <div>
      <button onClick={() => setOpen(!open)}>Toggle</button>
      <div
        style={{
          display: "flex",
          flexDirection: "row",
          alignItems: "stretch"
        }}
      >
        <div
          ref={ref}
          style={{ flexGrow: "1", backgroundColor: "teal", padding: "1em" }}
        >
          {width}x{height}
        </div>
        {renderDiv()}
        <div
          ref={ref3}
          style={{ flexGrow: "1", backgroundColor: "violet", padding: "1em" }}
        >
          {width3}x{height3}
        </div>
      </div>
    </div>
  );
};

@ZeeCoder
Copy link
Owner

ZeeCoder commented Jan 3, 2020

You're using object destructuring wrong, try something like:

const {ref, width: width2, height: height2} = resizeObserver()

Then it should work 👍

@ValentinKaisermayer
Copy link
Author

Thanks!

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

No branches or pull requests

2 participants