score:5
Accepted answer
class Bar extends React.Component {
render() {
return (
<div className='bar' style={{height: `${this.props.height / 2 }px`}} />
);
};
};
class Foo extends React.Component {
constructor() {
super();
this.state = { height: 0 };
this.updateHeight = this.updateHeight.bind(this);
}
componentDidMount() {
this.updateHeight();
window.addEventListener("resize", this.updateHeight);
}
componentWillUnmount() {
window.removeEventListener("resize", this.updateHeight);
}
componentDidUpdate() {
this.updateHeight();
}
updateHeight() {
if (this.state.height != this.div.clientHeight)
this.setState({ height: this.div.clientHeight })
}
render() {
return (
<div ref={ div => { this.div = div; } } className='foo'>
<Bar height={this.state.height} />
</div>
);
}
}
ReactDOM.render(<Foo/>, document.getElementById('app'));
Source: stackoverflow.com
Related Query
- Get component's height every time it renders
- Get logged user every time the component renders under React ContextAPI
- React Memo Renders Every Time with same given props
- React - Random position every time the same picture renders
- i get this message every time i try to update profile in my web app
- React re renders everything every time key is pressed
- Does getStaticProps fetch data every time I get back same page?
- React child re-renders every time parent renders
- Render Component every time it's get clicked
- Component calling function 10 times every time it renders
- Get the radio button value every time it changes in ReactJS
- How to pass the className prop to all components by default without defining it every time
- React formik Validation Error Api Renders Every Time when other field validate
- How do I get a random beer every time I reload the page?
- Get ID element from url and store it in a session array every time page loads
- ReactJS - Does render get called any time "setState" is called?
- Get viewport/window height in ReactJS
- ReactJS - Get Height of an element
- ReactJS get rendered component height
- Should we use useCallback in every function handler in React Functional Components
- react-router-dom get id from route with custom components and extra path
- How to get values from child components in React
- npx create-react-app takes too long every time
- Do I need to run "npm run build" every time I made changes?
- Can't get Jest to work with Styled Components which contain theming
- How to check when the react components get rendered or rerendered
- Have to reinstall node_modules every time I install a package
- How to trigger a CSS animation on EVERY TIME a react component re-renders
- All React-bootstrap Tabs render function gets called every time regardless of which tab was selected
- Get tests running time with Jest
More Query from same tag
- React hooks with Gatsby. onClick changes setState on second Click only
- Client side filtering using useReducer
- React useState, how to reset to initial value
- How do I edit form data in a React function component?
- Efficient way to request back-end for data on input change
- Why cannot use 'map or filter' in testing library with typescipt?
- empty POST request with react/axios
- Understanding Post request responses in React
- Cannot read property of null comparing state after state change
- I'm getting error using DataGrid Material UI component, it says "Can't resolve '@material-ui/x-license'", why?
- Add Image to DevExtreme React Grid Bootstrap4 Column
- REACT 16.3 - render props array as map (optionally)
- how to make conditional with props and react dynamic theme?
- fetch data from multiple apis with async await
- Reactjs loop from a database
- React Gatsby window is not defined server side
- React persistent state with React Router 4
- Move focus from one input to other input in react js when max length is full of first input
- Show one specific result in a list in React
- How to use wavesurfer.js in React?
- Eslint React Hooks Error: eslint-plugin-react-hooks exhaustive deps warning for a function dependency in useEffect
- How to toggle and add a styleName to a react component
- Search Bar fires but no changes take effect - React Hooks
- How to handle asynchronous calls from useEffect
- react change slide in slider after clicking a button
- How to move selection point to the beginning of input field?
- React Hooks Const Component vs Functional Component
- How to get specific row parameters and send it an API call in React Table?
- Use ENUMS dynamically for option TAG in SELECT (REACTJS)
- React : Toggling PopUp re-renders entire component