我試圖創建一個受控制的文本區域。反應道具undefined裏面的構造和存在渲染
class TextArea extends React.Component {
constructor(props) {
super(props);
this.state= {
text: this.props.initial
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
//some handle
}
render() {
return (
<textarea
value={this.state.text}
placeholder={this.props.initial}
onChange={this.handleChange}
/>
);
}
}
出於某種原因,如果我在構造函數中,我得到一個未定義的CONSOLE.LOG this.props.initial
。
但佔位符工作。
我想要做的是放棄佔位符,並設置一個初始值,以便用戶可以編輯,複製和交互。 (基本上是正常的文本,而不是佔位符,但我不能這樣做,因爲它不起作用)
我在做什麼錯了?
編輯: ,我路過props.initial
到textarea的方式:
<TextArea
initial={this.state.json.initial}
text={this.state.json.text}
changeHandler={this.handleChange}
/>
我正在從$ .getJSON調用中的JSON和我認爲JSON通話結束前,textarea的被渲染。是否有任何方法只能在componentWillMount函數之後運行渲染函數?
從'this.props'中刪除'this'並且它應該按預期工作。 – mersocarlin
你不能在你的構造函數中使用'props.initial'嗎? – Walk
超級建立這個正確的,所以這不應該改變任何東西 – Axnyff