Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ResizeObserver in React class component

I am using React 15 on Chrome and want to hook up an event listener to detect changes to a parent container. After looking around for options, I came across ResizeObserver and am not sure how to get it to work in my project.

Currently, I am putting it in my constructor but it does not seem to print any text and I am not sure what to put in the observe call.

class MyComponent extends React.Component {
    constructor(props) {
        super(props);

        const resizeObserver = new ResizeObserver((entries) => {
            console.log("Hello World");
        });

        resizeObserver.observe(somethingGoesHere);
    }

    render() {
        return (
            <AnotherComponent>
                <YetAnotherComponent>
                </YetAnotherComponent>

                <CanYouBelieveIt>
                </CanYouBelieveIt>

                <RealComponent />
            </AnotherComponent>
        );
    }
}

Ideally, I also don't want to wrap RealComponent in a div and give that div an id. Is there a way to the RealComponent directly?

My goal is to observe any resize changes to the RealComponent but MyComponent is fine too. What should I put in the somethingGoesHere slot?

EDIT:

For the sake of getting something to work, I bit the bullet and wrapped a div tag around RealComponent. I then gave it an id <div id="myDivTag"> and changed the observe call:

resizeObserver.observe(document.getElementById("myDivTag"));

However, when running this, I get:

Uncaught TypeError: resizeObserver.observe is not a function

Any help would be greatly appreciated.

like image 225
noblerare Avatar asked Jul 08 '19 20:07

noblerare


2 Answers

ComponentDidMount would be the best place to set up your observer but you also want to disconnect on ComponentWillUnmount.

class MyComponent extends React.Component {
  resizeObserver = null;
  resizeElement = createRef();

  componentDidMount() {
    this.resizeObserver = new ResizeObserver((entries) => {
      // do things
    });

    this.resizeObserver.observe(this.resizeElement.current);
  }

  componentWillUnmount() {
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
  }

  render() {
    return (
      <div ref={this.resizeElement}>
        ...
      </div>
    );
  }
}
like image 53
Davidicus Avatar answered Nov 03 '22 10:11

Davidicus


EDIT: Davidicus's answer below is more complete, look there first

ResizeObserver can't go in the constructor because the div doesn't exist at that point in the component lifecycle.

I don't think you can get around the extra div because react components reduce to html elements anyway.

Put this in componentDidMount and it should work:

componentDidMount() {
   const resizeObserver = new ResizeObserver((entries) => {
        console.log("Hello World");
   });

   resizeObserver.observe(document.getElementById("myDivTag"));
}
like image 15
Mark Avatar answered Nov 03 '22 09:11

Mark