Calling setState in componentDidUpdate for animations based on refs












0














Hi Stackoverflow community,



I'm doing some custom animations in my React app involving updating margins on a component with position: absolute. It's a menu that can toggle in and out.



I have a ref for the element it's toggling behind, whose node height I'm grabbing in componentDidMount. I use this height to know how far to move my toggling component.



I noticed when my app renders the first time my component isn't animating right because the height value is always zero. When I toggle it subsequently it works because the componentDidMount fires and updates the height of the other node properly.



To fix this I moved my divHeight field into the state and started calling setState in componentDidMount, now my toggleable component renders correctly each time.



Is this an anti-pattern? I can't see how else I would update my UI based on physical attributes of the HTML nodes, since those are only available through refs in componentDidMount...










share|improve this question



























    0














    Hi Stackoverflow community,



    I'm doing some custom animations in my React app involving updating margins on a component with position: absolute. It's a menu that can toggle in and out.



    I have a ref for the element it's toggling behind, whose node height I'm grabbing in componentDidMount. I use this height to know how far to move my toggling component.



    I noticed when my app renders the first time my component isn't animating right because the height value is always zero. When I toggle it subsequently it works because the componentDidMount fires and updates the height of the other node properly.



    To fix this I moved my divHeight field into the state and started calling setState in componentDidMount, now my toggleable component renders correctly each time.



    Is this an anti-pattern? I can't see how else I would update my UI based on physical attributes of the HTML nodes, since those are only available through refs in componentDidMount...










    share|improve this question

























      0












      0








      0







      Hi Stackoverflow community,



      I'm doing some custom animations in my React app involving updating margins on a component with position: absolute. It's a menu that can toggle in and out.



      I have a ref for the element it's toggling behind, whose node height I'm grabbing in componentDidMount. I use this height to know how far to move my toggling component.



      I noticed when my app renders the first time my component isn't animating right because the height value is always zero. When I toggle it subsequently it works because the componentDidMount fires and updates the height of the other node properly.



      To fix this I moved my divHeight field into the state and started calling setState in componentDidMount, now my toggleable component renders correctly each time.



      Is this an anti-pattern? I can't see how else I would update my UI based on physical attributes of the HTML nodes, since those are only available through refs in componentDidMount...










      share|improve this question













      Hi Stackoverflow community,



      I'm doing some custom animations in my React app involving updating margins on a component with position: absolute. It's a menu that can toggle in and out.



      I have a ref for the element it's toggling behind, whose node height I'm grabbing in componentDidMount. I use this height to know how far to move my toggling component.



      I noticed when my app renders the first time my component isn't animating right because the height value is always zero. When I toggle it subsequently it works because the componentDidMount fires and updates the height of the other node properly.



      To fix this I moved my divHeight field into the state and started calling setState in componentDidMount, now my toggleable component renders correctly each time.



      Is this an anti-pattern? I can't see how else I would update my UI based on physical attributes of the HTML nodes, since those are only available through refs in componentDidMount...







      javascript css reactjs state






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 0:53









      Grandclosing

      346319




      346319
























          1 Answer
          1






          active

          oldest

          votes


















          0














          There is another method to perform physical changes in html elements instead of using refs. Instead you can toggle classes on and off elements to do what you want, like toggling a menu in and out of frame.



          Below is an example of how its done without the use of refs. Click the button and you can imagine the red div is your top navigation menu.






          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








          share|improve this answer





















          • Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
            – Grandclosing
            Nov 23 '18 at 1:31










          • Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
            – Shawn Andrews
            Nov 23 '18 at 2:11











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439507%2fcalling-setstate-in-componentdidupdate-for-animations-based-on-refs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          There is another method to perform physical changes in html elements instead of using refs. Instead you can toggle classes on and off elements to do what you want, like toggling a menu in and out of frame.



          Below is an example of how its done without the use of refs. Click the button and you can imagine the red div is your top navigation menu.






          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








          share|improve this answer





















          • Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
            – Grandclosing
            Nov 23 '18 at 1:31










          • Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
            – Shawn Andrews
            Nov 23 '18 at 2:11
















          0














          There is another method to perform physical changes in html elements instead of using refs. Instead you can toggle classes on and off elements to do what you want, like toggling a menu in and out of frame.



          Below is an example of how its done without the use of refs. Click the button and you can imagine the red div is your top navigation menu.






          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








          share|improve this answer





















          • Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
            – Grandclosing
            Nov 23 '18 at 1:31










          • Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
            – Shawn Andrews
            Nov 23 '18 at 2:11














          0












          0








          0






          There is another method to perform physical changes in html elements instead of using refs. Instead you can toggle classes on and off elements to do what you want, like toggling a menu in and out of frame.



          Below is an example of how its done without the use of refs. Click the button and you can imagine the red div is your top navigation menu.






          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








          share|improve this answer












          There is another method to perform physical changes in html elements instead of using refs. Instead you can toggle classes on and off elements to do what you want, like toggling a menu in and out of frame.



          Below is an example of how its done without the use of refs. Click the button and you can imagine the red div is your top navigation menu.






          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>








          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>





          class MenuExample extends React.Component {
          constructor(props) {
          super(props);
          this.onToggleMenu = this.onToggleMenu.bind(this);
          this.state = { menuToggle: false };
          }

          onToggleMenu(e) {
          this.setState({ menuToggle: !this.state.menuToggle });
          }

          render() {
          return (
          <div>
          <button onClick={this.onToggleMenu}>Open menu</button>
          <div className={`menu ${this.state.menuToggle && 'active'}`} />
          </div>
          );
          }
          }

          // Render it
          ReactDOM.render(
          <MenuExample/>,
          document.getElementById("react")
          );

          button {
          position: absolute;
          top: 150px;
          }

          .menu {
          position: absolute;
          top: -100px;
          left: 0px;
          width: 100%;
          height: 100px;
          background-color: red;
          transition: all .3s ease-in;
          -webkit-transition: all .3s ease-in;
          }

          .menu.active {
          top: 0px;
          }

          <div id="react"></div>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 1:17









          Shawn Andrews

          945616




          945616












          • Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
            – Grandclosing
            Nov 23 '18 at 1:31










          • Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
            – Shawn Andrews
            Nov 23 '18 at 2:11


















          • Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
            – Grandclosing
            Nov 23 '18 at 1:31










          • Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
            – Shawn Andrews
            Nov 23 '18 at 2:11
















          Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
          – Grandclosing
          Nov 23 '18 at 1:31




          Thanks for the quick response! The issue with this is that my elements don't have fixed heights - so I wouldn't be able to do something like top: -100px, so I need to get their heights after they've rendered.
          – Grandclosing
          Nov 23 '18 at 1:31












          Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
          – Shawn Andrews
          Nov 23 '18 at 2:11




          Hmmm. If the height of the item varies, then i can only suggest you put a min and max-height on the item, then top: - max-height guarantees the item will be hidden while allowing for some variable height
          – Shawn Andrews
          Nov 23 '18 at 2:11


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439507%2fcalling-setstate-in-componentdidupdate-for-animations-based-on-refs%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Sphinx de Gizeh

          Dijon

          Guerrita