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
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
add a comment |
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
add a comment |
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
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
javascript reactjs
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
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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/>
)
this.topNav.getHeight()
returns 0 for me, also withthis.topNav.state.height
– Carl Binalla
Mar 8 at 8:27
I'm putting someconsole.log()
, and it seems like thecomponentDidMount()
of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed
– Carl Binalla
Mar 8 at 8:55
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
edited Mar 15 at 19:10
answered Mar 8 at 8:42
Marcelo RafaelMarcelo Rafael
473313
473313
add a comment |
add a comment |
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/>
)
this.topNav.getHeight()
returns 0 for me, also withthis.topNav.state.height
– Carl Binalla
Mar 8 at 8:27
I'm putting someconsole.log()
, and it seems like thecomponentDidMount()
of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed
– Carl Binalla
Mar 8 at 8:55
add a comment |
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/>
)
this.topNav.getHeight()
returns 0 for me, also withthis.topNav.state.height
– Carl Binalla
Mar 8 at 8:27
I'm putting someconsole.log()
, and it seems like thecomponentDidMount()
of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed
– Carl Binalla
Mar 8 at 8:55
add a comment |
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/>
)
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/>
)
answered Mar 8 at 8:15
Matei MihaiMatei Mihai
19.4k22642
19.4k22642
this.topNav.getHeight()
returns 0 for me, also withthis.topNav.state.height
– Carl Binalla
Mar 8 at 8:27
I'm putting someconsole.log()
, and it seems like thecomponentDidMount()
of Dashboard.js is the first to fire before the state in FixedTopNavigation.js has been changed
– Carl Binalla
Mar 8 at 8:55
add a comment |
this.topNav.getHeight()
returns 0 for me, also withthis.topNav.state.height
– Carl Binalla
Mar 8 at 8:27
I'm putting someconsole.log()
, and it seems like thecomponentDidMount()
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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