【发布时间】:2020-12-01 20:50:09
【问题描述】:
在我的 reactjs 类组件中,我想创建一个按钮,每次单击它时都会打开一个新的文本区域(例如,当我单击 5 次时,它应该打开 5 个文本区域)。在当前结果中,它只打开一个文本区域。
因此,在第一步中,我创建了一个值为 0 的状态,并创建了一个应该改变状态的函数:
// InitialState
state = {
value: 0,
};
onChange() {
this.setState({
value: this.state.value + 1,
});
}
在下一步中,我渲染了一个按钮并创建了 if 语句来显示文本区域(这不起作用):
render() {
return (
<div>
<IconButton onClick={this.onChange.bind(this)}>
<AddCircleOutlineIcon />
</IconButton>
<br />
{this.state.value >= 1 ? this.showTextArea() : null}
</div>
);
}
这是我的 showTextArea 函数:
showTextArea = () => {
return (
<textarea
placeholder={this.state.placeholder}
value={this.state.value}
onChange={this.onChange1.bind(this)}
rows="2"
style={{ fontSize: "18px" }}
onFocus={(e) => (e.target.placeholder = "")}
onBlur={(e) => (e.target.placeholder = this.state.placeholder)}
/>
);
};
【问题讨论】:
标签: javascript reactjs react-state react-state-management