简体   繁体   English

如何使用反应编辑元素宽度

[英]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).在我的代码中,我在组件状态 (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!谢谢各位!

You can use inline style prop.您可以使用内联样式道具。

Example示例

  render() {
    return <div className="bar" >
      <div className="progress" style={{width: this.state.progress}} >
        // ...
      </div> 
    </div>;
  }

尝试将style={{width: this.state.progress + '%'}}progress类的 div

Assuming that you are going to update progress with a call like this:假设您要通过这样的调用更新进度:

this.setState({ progress: 50})

Setting the state will trigger a re-render, which can control the width of your progress bar like this:设置状态将触发重新渲染,它可以像这样控制进度条的宽度:

  <div className = "progress" style={{width: this.state.progress+"%"}}>

您可以通过style属性将进度条的百分比值指定为 CSS 元素的百分比宽度:

 <span className="fill" style={{width: this.state.value + '%'}}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM