Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit element width using react

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!

like image 919
Koop4 Avatar asked Oct 24 '17 10:10

Koop4


People also ask

How do you set the width of a element in React?

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.

How do you change the size of a div in React?

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!

How do I change the width of an image in React?

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.

How do I change the width of a percentage in React JS?

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.


Video Answer


1 Answers

You can use inline style prop.

Example

  render() {
    return <div className="bar" >
      <div className="progress" style={{width: this.state.progress}} >
        // ...
      </div> 
    </div>;
  }
like image 56
bennygenel Avatar answered Oct 02 '22 09:10

bennygenel