-1
(我使用JSX与ES6语法)在使用道具阵营style属性
这工作:
render() {
return (
<div style={{ width: '95%' }}></div>
)
}
这不起作用:(为什么不呢?)编辑:它实际上做的工作
render() {
return (
<div style={{ width: this.props.progress + '%' }}></div>
)
}
编辑:
它可以工作,但样式值必须是有效的值,否则会返回错误。
我使用状态来创建样式对象,并用构造函数中的正则表达式清除属性,所以它不会因错误的值而再次出错。这是我的解决方案:
import React, { Component, PropTypes } from 'react'
export default class ProgressBar extends Component {
constructor(props) {
super(props)
let progress = +props.progress.replace(/[^0-9]/g, '') || 50
this.state = {
style: {
width: progress + '%'
}
}
}
render() {
return (
...
<div className="progress-bar" role="progressbar" aria-valuenow={ this.state.progress } style={ this.state.style }></div>
...
)
}
}
ProgressBar.propTypes = {
progress: PropTypes.string.isRequired
}
取决于'this.props.progress'的价值。 –
@JoeClay它无论是字符串或数字,你会得到'95%'。你的例子适用于我 –
@TheReason:是的,我只是想知道他是否得到了“undefined”或什么东西。 –