无法更新具有状态的 React Textarea
React Textarea with state can't be update
我正在开发 ReactJs 应用程序。这里我有一个 Textarea
连接到 state
<Textarea className="c-input" tabIndex="7" minRows={4}
ref="adMessage"
onKeyPress={ console.log('keypress textarea') }
onChange={ console.log('changed text area') }
placeholder=""
value={this.state.adDescription}
/>
在另一个控制器中,我有一些标签,点击它们,我可以像这样更新 adDescription
状态
handelSetDescription = (label) => {
let message = '';
if (this.state.adDescription.length === 0){
message = this.state.adDescription + label;
} else if (this.state.adDescription.length > 0){
message = this.state.adDescription + ", " + label;
}
this.setState({
adDescription: message
});
};
但我也想手动向文本区域添加更多文本,但似乎无法在其中添加任何文本。当我点击 Textarea 并输入内容时,它什么也没做。
如何手动添加文本?
根据您的业务逻辑尝试做这样的事情。
<Textarea className="c-input" tabIndex="7" minRows={4}
ref="adMessage"
onKeyPress={ console.log('keypress textarea') }
onChange={ this.someFunctionName }
placeholder=""
value={this.state.adDescription}
/>
函数可以做如下事情:
changeStateForDescription = (val) => {
this.setState({
adDescription: val
});
}
handelSetDescription = (label) => {
let message = '';
if (this.state.adDescription.length === 0){
message = this.state.adDescription + label;
} else if (this.state.adDescription.length > 0){
message = this.state.adDescription + ", " + label;
}
this.changeStateForDescription(message);
};
someFunctionName = (e) => {
this.changeStateForDescription(this.state.adDescription + e.target.value);
}
我正在开发 ReactJs 应用程序。这里我有一个 Textarea
连接到 state
<Textarea className="c-input" tabIndex="7" minRows={4}
ref="adMessage"
onKeyPress={ console.log('keypress textarea') }
onChange={ console.log('changed text area') }
placeholder=""
value={this.state.adDescription}
/>
在另一个控制器中,我有一些标签,点击它们,我可以像这样更新 adDescription
状态
handelSetDescription = (label) => {
let message = '';
if (this.state.adDescription.length === 0){
message = this.state.adDescription + label;
} else if (this.state.adDescription.length > 0){
message = this.state.adDescription + ", " + label;
}
this.setState({
adDescription: message
});
};
但我也想手动向文本区域添加更多文本,但似乎无法在其中添加任何文本。当我点击 Textarea 并输入内容时,它什么也没做。
如何手动添加文本?
根据您的业务逻辑尝试做这样的事情。
<Textarea className="c-input" tabIndex="7" minRows={4}
ref="adMessage"
onKeyPress={ console.log('keypress textarea') }
onChange={ this.someFunctionName }
placeholder=""
value={this.state.adDescription}
/>
函数可以做如下事情:
changeStateForDescription = (val) => {
this.setState({
adDescription: val
});
}
handelSetDescription = (label) => {
let message = '';
if (this.state.adDescription.length === 0){
message = this.state.adDescription + label;
} else if (this.state.adDescription.length > 0){
message = this.state.adDescription + ", " + label;
}
this.changeStateForDescription(message);
};
someFunctionName = (e) => {
this.changeStateForDescription(this.state.adDescription + e.target.value);
}