我想根據我發送給組件的專業人員動態設置類。不知何故,我得到錯誤「無法讀取屬性'未定義狀態」。 我想這不存在,當我嘗試設置類的狀態類作爲一個類?在使用組件的渲染之前,我是否必須重新綁定它?設置類使用狀態如果其他與反應獲取狀態未定義的錯誤
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'方法內:) –
謝謝,但我超級新的反應,你能給我一個更大的代碼示例你是什麼意思的箭頭功能? –