I'd like to edit my progress bar width by using react props. In my code I have a certain value in the component state (state.progress). How can I use it to properly stretch the progress bar,
class Hello extends React.Component {
render() {
return <div className = "bar" >
<div className = "progress" >
</div>
</div>;
}
}
ReactDOM.render( <
Hello name = "World" / > ,
document.getElementById('container')
);
.bar {
width: 100%;
border: 1px solid black;
border-radius: 15px;
height: 15px;
}
.progress {
width: 5%;
background: green;
border-radius: 15px;
height: 15px;
}
<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>
<div id="container">
<!-- This element's contents will be replaced with your component. -->
</div>
Thanks folks!
To get the width of an Element in React: Set the ref prop on the element. In the useLayoutEffect hook, update the state variable for the width. Use the offsetWidth property to get the width of the element.
To set a Div's height to cover the full screen in React, set its height to 100vh , e.g. <div style={{height: '100vh'}}>... </div> . When the height of the element is set to 100vh , it covers 100% of the viewport's height. Copied!
To resize images in React, developers use CSS properties like min-height , max-height , min-width , and max-width . As you can probably guess, min-height and min-width set the image's basic minimum size. The elements will not become smaller than the specified values regardless of the user's screen size.
To set width style to a percentage number with React Native, we can use flexbox. to set flexDirection to 'row' to add a horizontal flexbox layout. We set the 2nd Text component to fill 20% of the screen. Then we set flexGrow to 1 on the first Text component to expand to fill the remaining width of the screen.
You can use inline style prop.
Example
render() {
return <div className="bar" >
<div className="progress" style={{width: this.state.progress}} >
// ...
</div>
</div>;
}
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