我想根据我发送给组件的专业人员动态设置类。不知何故,我得到错误“无法读取属性'未定义状态”。 我想这不存在,当我尝试设置类的状态类作为一个类?在使用组件的渲染之前,我是否必须重新绑定它?设置类使用状态如果其他与反应获取状态未定义的错误
var ReactDOM = require('react-dom');
var React = require('react');
class Button extends React.Component {
constructor(props) {
super(props);
console.log("BUTTON")
console.log(props);
this.state = {
class: "small-button"
};
props.options.map(function (option) {
if (option.Description > 10) {
this.setState({
class: "big-button"
});
}
});
console.log("STATE: " + this.state.class);
}
render() {
if (this.props.options) {
return (<div> {
this.props.options.map(function (option) {
return <div className={ this.state.class === 'big-button' ? 'option-button big-button' : 'option-button small-button'} key={option.Id}> {option.Description}</div>
})
}
</div>
)
} else {
return <div>No options defined</div>
}
}
}
module.exports = Button;
其的结合问题,使用'箭头function':'this.props.options.map((选项)=> {.....'**一个建议:**不要将逻辑放入'constructor'中,也不要使用'setState',使用生命周期方法。将该逻辑放在'componentDidMount'方法或'componentWillMount'方法内:) –
谢谢,但我超级新的反应,你能给我一个更大的代码示例你是什么意思的箭头功能? –