Getting the height of an imported componentHow do you get a timestamp in JavaScript?How to get the children of the $(this) selector?Get the name of an object's typeGet current URL with jQuery?How can I get query string values in JavaScript?Get the current URL with JavaScript?Get selected value in dropdown list using JavaScript?How do I get the current date in JavaScript?Get selected text from a drop-down list (select box) using jQueryGet the size of the screen, current web page and browser window

What (else) happened July 1st 1858 in London?

Could solar power be utilized and substitute coal in the 19th century?

Greatest common substring

Can a Bard use an arcane focus?

Adding empty element to declared container without declaring type of element

Is it possible to build a CPA Secure encryption scheme which remains secure even when the encryption of secret key is given?

Simulating a probability of 1 of 2^N with less than N random bits

How can I raise concerns with a new DM about XP splitting?

Word describing multiple paths to the same abstract outcome

Why does this part of the Space Shuttle launch pad seem to be floating in air?

Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)

Lifted its hind leg on or lifted its hind leg towards?

Pronouncing Homer as in modern Greek

I2C signal and power over long range (10meter cable)

My boss asked me to take a one-day class, then signs it up as a day off

Freedom of speech and where it applies

Visiting the UK as unmarried couple

What is the term when two people sing in harmony, but they aren't singing the same notes?

Science Fiction story where a man invents a machine that can help him watch history unfold

Would it be legal for a US State to ban exports of a natural resource?

Can the harmonic series explain the origin of the major scale?

"lassen" in meaning "sich fassen"

Why isn't KTEX's runway designation 10/28 instead of 9/27?

Why are all the doors on Ferenginar (the Ferengi home world) far shorter than the average Ferengi?



Getting the height of an imported component


How do you get a timestamp in JavaScript?How to get the children of the $(this) selector?Get the name of an object's typeGet current URL with jQuery?How can I get query string values in JavaScript?Get the current URL with JavaScript?Get selected value in dropdown list using JavaScript?How do I get the current date in JavaScript?Get selected text from a drop-down list (select box) using jQueryGet the size of the screen, current web page and browser window













0















How to get the getBoundingClientRect() of an imported component? or at least access the elements inside



I can get it if it used inside the component itself



FixedTopNavigation.js



constructor(props) 
super(props);
this.state =
height: 0,



componentDidMount()
console.log(this.topNav.getBoundingClientRect());

render()
return (
<nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
<span className="navbar-toggler-icon"></span>
</button>
<a className="navbar-brand" href="/">Biometrics</a>
</nav>

)



But when used after importing that component in a different file:



Dashboard.js



constructor(props) 
super(props);
this.state =
height: 0,



componentDidMount()
console.log(this.topNav.getBoundingClientRect());

render()
return (
<FixedTopNavigation ref=(elem) => this.topNav = elem/>
)



Error received:




TypeError: this.topNav.getBoundingClientRect is not a function




After using console.log(), it seems that it is not returning the element anymore. All tutorials that I searched points to the first code syntax.



Also tried this.topNav = React.createRef(), but the error is still the same










share|improve this question




























    0















    How to get the getBoundingClientRect() of an imported component? or at least access the elements inside



    I can get it if it used inside the component itself



    FixedTopNavigation.js



    constructor(props) 
    super(props);
    this.state =
    height: 0,



    componentDidMount()
    console.log(this.topNav.getBoundingClientRect());

    render()
    return (
    <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
    <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
    <span className="navbar-toggler-icon"></span>
    </button>
    <a className="navbar-brand" href="/">Biometrics</a>
    </nav>

    )



    But when used after importing that component in a different file:



    Dashboard.js



    constructor(props) 
    super(props);
    this.state =
    height: 0,



    componentDidMount()
    console.log(this.topNav.getBoundingClientRect());

    render()
    return (
    <FixedTopNavigation ref=(elem) => this.topNav = elem/>
    )



    Error received:




    TypeError: this.topNav.getBoundingClientRect is not a function




    After using console.log(), it seems that it is not returning the element anymore. All tutorials that I searched points to the first code syntax.



    Also tried this.topNav = React.createRef(), but the error is still the same










    share|improve this question


























      0












      0








      0








      How to get the getBoundingClientRect() of an imported component? or at least access the elements inside



      I can get it if it used inside the component itself



      FixedTopNavigation.js



      constructor(props) 
      super(props);
      this.state =
      height: 0,



      componentDidMount()
      console.log(this.topNav.getBoundingClientRect());

      render()
      return (
      <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
      <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
      </button>
      <a className="navbar-brand" href="/">Biometrics</a>
      </nav>

      )



      But when used after importing that component in a different file:



      Dashboard.js



      constructor(props) 
      super(props);
      this.state =
      height: 0,



      componentDidMount()
      console.log(this.topNav.getBoundingClientRect());

      render()
      return (
      <FixedTopNavigation ref=(elem) => this.topNav = elem/>
      )



      Error received:




      TypeError: this.topNav.getBoundingClientRect is not a function




      After using console.log(), it seems that it is not returning the element anymore. All tutorials that I searched points to the first code syntax.



      Also tried this.topNav = React.createRef(), but the error is still the same










      share|improve this question
















      How to get the getBoundingClientRect() of an imported component? or at least access the elements inside



      I can get it if it used inside the component itself



      FixedTopNavigation.js



      constructor(props) 
      super(props);
      this.state =
      height: 0,



      componentDidMount()
      console.log(this.topNav.getBoundingClientRect());

      render()
      return (
      <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
      <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
      <span className="navbar-toggler-icon"></span>
      </button>
      <a className="navbar-brand" href="/">Biometrics</a>
      </nav>

      )



      But when used after importing that component in a different file:



      Dashboard.js



      constructor(props) 
      super(props);
      this.state =
      height: 0,



      componentDidMount()
      console.log(this.topNav.getBoundingClientRect());

      render()
      return (
      <FixedTopNavigation ref=(elem) => this.topNav = elem/>
      )



      Error received:




      TypeError: this.topNav.getBoundingClientRect is not a function




      After using console.log(), it seems that it is not returning the element anymore. All tutorials that I searched points to the first code syntax.



      Also tried this.topNav = React.createRef(), but the error is still the same







      javascript reactjs






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 8:51









      Josh Pittman

      2,00511231




      2,00511231










      asked Mar 8 at 8:06









      Carl BinallaCarl Binalla

      4,23142040




      4,23142040






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Theres some good ways to do what u want, like Redux, Context, a global variable, example: window.myApp.topNavBar.height = 100



          I would probably go with this one:



          import React from "react";
          import render from "react-dom";

          class Target extends React.Component
          render()
          return <h1 className=this.props.className>TARGET</h1>;



          class App extends React.Component
          componentDidMount()
          console.log(
          "RESULT: ",
          document.querySelector(".target").getBoundingClientRect()
          );

          render()
          return <Target className="target" />;



          render(<App />, document.querySelector(".root"));


          U can even remove the property.

          U can see this working here: https://codesandbox.io/s/5xx50pw984?fontsize=14



          Edit



          For those which are using a state manager like Redux, please avoid using this kind of code and try to put everything inside ur redux state. And I would recommend to avoid the react state. U can read
          this article






          share|improve this answer
































            0














            I think you could try something like this:



            constructor(props)

            super(props);

            this.state =
            height: 0,



            componentDidMount()

            this.setState(
            height: this.topNav.getBoundingClientRect()
            );


            getHeight()

            return this.state.height;


            render()

            return (
            <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
            <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
            <span className="navbar-toggler-icon"></span>
            </button>
            <a className="navbar-brand" href="/">Biometrics</a>
            </nav>

            )



            And when you want to access the component's height then you would do something like:



            constructor(props)

            super(props);

            this.state =
            height: 0,



            componentDidMount()

            this.setState(
            height: this.topNav.getHeight()
            );


            render()

            return (
            <FixedTopNavigation ref=(elem) => this.topNav = elem/>
            )






            share|improve this answer























            • this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

              – Carl Binalla
              Mar 8 at 8:27











            • I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

              – Carl Binalla
              Mar 8 at 8:55










            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%2f55059013%2fgetting-the-height-of-an-imported-component%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Theres some good ways to do what u want, like Redux, Context, a global variable, example: window.myApp.topNavBar.height = 100



            I would probably go with this one:



            import React from "react";
            import render from "react-dom";

            class Target extends React.Component
            render()
            return <h1 className=this.props.className>TARGET</h1>;



            class App extends React.Component
            componentDidMount()
            console.log(
            "RESULT: ",
            document.querySelector(".target").getBoundingClientRect()
            );

            render()
            return <Target className="target" />;



            render(<App />, document.querySelector(".root"));


            U can even remove the property.

            U can see this working here: https://codesandbox.io/s/5xx50pw984?fontsize=14



            Edit



            For those which are using a state manager like Redux, please avoid using this kind of code and try to put everything inside ur redux state. And I would recommend to avoid the react state. U can read
            this article






            share|improve this answer





























              1














              Theres some good ways to do what u want, like Redux, Context, a global variable, example: window.myApp.topNavBar.height = 100



              I would probably go with this one:



              import React from "react";
              import render from "react-dom";

              class Target extends React.Component
              render()
              return <h1 className=this.props.className>TARGET</h1>;



              class App extends React.Component
              componentDidMount()
              console.log(
              "RESULT: ",
              document.querySelector(".target").getBoundingClientRect()
              );

              render()
              return <Target className="target" />;



              render(<App />, document.querySelector(".root"));


              U can even remove the property.

              U can see this working here: https://codesandbox.io/s/5xx50pw984?fontsize=14



              Edit



              For those which are using a state manager like Redux, please avoid using this kind of code and try to put everything inside ur redux state. And I would recommend to avoid the react state. U can read
              this article






              share|improve this answer



























                1












                1








                1







                Theres some good ways to do what u want, like Redux, Context, a global variable, example: window.myApp.topNavBar.height = 100



                I would probably go with this one:



                import React from "react";
                import render from "react-dom";

                class Target extends React.Component
                render()
                return <h1 className=this.props.className>TARGET</h1>;



                class App extends React.Component
                componentDidMount()
                console.log(
                "RESULT: ",
                document.querySelector(".target").getBoundingClientRect()
                );

                render()
                return <Target className="target" />;



                render(<App />, document.querySelector(".root"));


                U can even remove the property.

                U can see this working here: https://codesandbox.io/s/5xx50pw984?fontsize=14



                Edit



                For those which are using a state manager like Redux, please avoid using this kind of code and try to put everything inside ur redux state. And I would recommend to avoid the react state. U can read
                this article






                share|improve this answer















                Theres some good ways to do what u want, like Redux, Context, a global variable, example: window.myApp.topNavBar.height = 100



                I would probably go with this one:



                import React from "react";
                import render from "react-dom";

                class Target extends React.Component
                render()
                return <h1 className=this.props.className>TARGET</h1>;



                class App extends React.Component
                componentDidMount()
                console.log(
                "RESULT: ",
                document.querySelector(".target").getBoundingClientRect()
                );

                render()
                return <Target className="target" />;



                render(<App />, document.querySelector(".root"));


                U can even remove the property.

                U can see this working here: https://codesandbox.io/s/5xx50pw984?fontsize=14



                Edit



                For those which are using a state manager like Redux, please avoid using this kind of code and try to put everything inside ur redux state. And I would recommend to avoid the react state. U can read
                this article







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 15 at 19:10

























                answered Mar 8 at 8:42









                Marcelo RafaelMarcelo Rafael

                473313




                473313























                    0














                    I think you could try something like this:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getBoundingClientRect()
                    );


                    getHeight()

                    return this.state.height;


                    render()

                    return (
                    <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
                    <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                    </button>
                    <a className="navbar-brand" href="/">Biometrics</a>
                    </nav>

                    )



                    And when you want to access the component's height then you would do something like:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getHeight()
                    );


                    render()

                    return (
                    <FixedTopNavigation ref=(elem) => this.topNav = elem/>
                    )






                    share|improve this answer























                    • this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

                      – Carl Binalla
                      Mar 8 at 8:27











                    • I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

                      – Carl Binalla
                      Mar 8 at 8:55















                    0














                    I think you could try something like this:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getBoundingClientRect()
                    );


                    getHeight()

                    return this.state.height;


                    render()

                    return (
                    <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
                    <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                    </button>
                    <a className="navbar-brand" href="/">Biometrics</a>
                    </nav>

                    )



                    And when you want to access the component's height then you would do something like:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getHeight()
                    );


                    render()

                    return (
                    <FixedTopNavigation ref=(elem) => this.topNav = elem/>
                    )






                    share|improve this answer























                    • this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

                      – Carl Binalla
                      Mar 8 at 8:27











                    • I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

                      – Carl Binalla
                      Mar 8 at 8:55













                    0












                    0








                    0







                    I think you could try something like this:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getBoundingClientRect()
                    );


                    getHeight()

                    return this.state.height;


                    render()

                    return (
                    <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
                    <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                    </button>
                    <a className="navbar-brand" href="/">Biometrics</a>
                    </nav>

                    )



                    And when you want to access the component's height then you would do something like:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getHeight()
                    );


                    render()

                    return (
                    <FixedTopNavigation ref=(elem) => this.topNav = elem/>
                    )






                    share|improve this answer













                    I think you could try something like this:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getBoundingClientRect()
                    );


                    getHeight()

                    return this.state.height;


                    render()

                    return (
                    <nav ref=(elem) => this.topNav = elem id="topNav" className="fixed-top navbar navbar-expand-md navbar-light bg-light">
                    <button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#sideNav" aria-controls="navbarTogglerDemo03" aria-expanded="false" aria-label="Toggle navigation">
                    <span className="navbar-toggler-icon"></span>
                    </button>
                    <a className="navbar-brand" href="/">Biometrics</a>
                    </nav>

                    )



                    And when you want to access the component's height then you would do something like:



                    constructor(props)

                    super(props);

                    this.state =
                    height: 0,



                    componentDidMount()

                    this.setState(
                    height: this.topNav.getHeight()
                    );


                    render()

                    return (
                    <FixedTopNavigation ref=(elem) => this.topNav = elem/>
                    )







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 8 at 8:15









                    Matei MihaiMatei Mihai

                    19.4k22642




                    19.4k22642












                    • this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

                      – Carl Binalla
                      Mar 8 at 8:27











                    • I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

                      – Carl Binalla
                      Mar 8 at 8:55

















                    • this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

                      – Carl Binalla
                      Mar 8 at 8:27











                    • I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

                      – Carl Binalla
                      Mar 8 at 8:55
















                    this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

                    – Carl Binalla
                    Mar 8 at 8:27





                    this.topNav.getHeight() returns 0 for me, also with this.topNav.state.height

                    – Carl Binalla
                    Mar 8 at 8:27













                    I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

                    – Carl Binalla
                    Mar 8 at 8:55





                    I'm putting some console.log(), and it seems like the componentDidMount() of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed

                    – Carl Binalla
                    Mar 8 at 8:55

















                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059013%2fgetting-the-height-of-an-imported-component%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

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme