I'm making a react redux app.
My view gets bigger as I add elements (or smaller as I remove them) but I can't get my background to follow suit correctly.
I've tried using scrollHeight to determine the size it should have :
https://i.imgur.com/HGHJgub.gifv
Here is my code :
constructor(props) {
super(props);
this.state = {
heightSet: 0,
};
this.updateDimensions = this.updateDimensions.bind(this);
}
componentDidMount() {
this.updateDimensions();
window.addEventListener('resize', this.updateDimensions);
}
componentWillUnmount() {
window.removeEventListener('resize', this.updateDimensions);
}
updateDimensions() {
this.setState({ heightSet: document.body.scrollHeight });
console.log(document.body.scrollHeight);
}
render() {
const divStyle = {
height: this.state.heightSet + 'px',
};
return (
<div style={divStyle}>
</div>
)
}
but All of this is clearly to be ditched. I'm not taking the right approach. It also touches on another issue of my app : It knows to add height to the view but not to remove it. Anybody know why it has this behavior and how to remedy it?
UPDATE :
CLARIFICATION the real issue is that this solution doesn't have any update on the var when I click "add component" and my scroll height increases. all in all the above solution is utter garbage. I like this idea : Set color for extra page parts visible during rubber band scroll (yeah it's a hack but that's fine by me)
from Shishir Arora and tksb
but it seems it does not work on modern browsers (at least not Chrome and latest Chrome is target N°1 for my app).
To set the height of a text area dynamically with JavaScript and React, we can set the height of the text area when we type into it. to set the onKeyDown prop to the handleKeyDown function. In it, we set the height of the text area to the e. target.
In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.
To get the height of an Element in React: Set the ref prop on the element. In the useLayoutEffect hook, update the state variable for the height. Use the clientHeight property to get the height of the element.
I reproduced the scenario and I got to this. Hope it helps.
Why is it always growing?
In my case, document.body.scrollHeight
is returning the height of the body with its margin (for some reason), so every time the component height is set it keeps being smaller than the body's scrollHeight and given that the body grows with its children, with the resize, the component just keeps growing.
For Example:
componentDidMount :
bodyHeight = 90 + 10 (a margin)
scrollHeight = 100
componentHeight = 100
newBodyHeight = 100 + 10
Resize:
bodyHeight = 100 + 10
scrollHeight = 110
componentHeight = 110
newBodyHeight = 110 + 10 , and so on.
How to remedy it?
You could subtract the margin of the body from the scrollHeight or calculate the height from the component's children's height.
I just changed this:
updateDimensions() {
const margin = 16; // In my case was marginTop: 8px and marginBottom: 8px
const heightSet = document.body.scrollHeight - margin;
this.setState({ heightSet });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With