我的 React 应用程序中有一个文本区域,其中填充了一个值。我希望更新此文本区域并提交表单以更新数据库中的行。
<textarea id="description" className="materialize-textarea" length="120" value={description} disabled={isDisabled}></textarea>
描述变量用数据库中的值填充文本区域。该字段未被禁用。
我尝试附加一个 onChange 事件,该事件调度一个动作(redux)来更改文本区域的值,但它会为每个字母触发,而且速度太慢。
如何创建一个由值填充且可编辑的文本区域?
谢谢你!
这是一个带有两个文本区域的组件。 props 和 state 之间存在受控关系。这是输入的关键。注意 componentWillReceiveProps。
import React, {Component} from 'react';
import Actions from '../../flux/Actions';
let CurrentSnipDivSty = {
height: 'calc(((100% - 40px) * .4) - 3px)',
minHeight: '9.5em',
width: '100%'
};
let CurrentSnipTextAreaSty = {
backgroundColor: '#213919',
border: '1px solid #314929',
color: '#afac87',
fontSize: '1em',
height: 'calc(50% - 20px)',
overflowY: 'auto',
padding: '5px',
width: 'calc(100% - 12px)'
};
class SnipsDetailRender extends Component {
render() {
let snip = this.state.snip.snip;
let comment = this.state.snip.comment;
return (
<div id='CurrentSnipDivSty' style={CurrentSnipDivSty}>
<textarea
value={snip}
onChange={this.handleSnipChange}
style={CurrentSnipTextAreaSty} />
<textarea
value={comment}
onChange={this.handleCommentChange}
style={CurrentSnipTextAreaSty} />
</div>
);
}
}
export default class SnipsDetail extends SnipsDetailRender {
constructor() {
super();
this.state = { snip: {snip: '', comment: ''} };
}
componentWillReceiveProps = (nextProps) => {
if ((nextProps.data.snip != this.state.snip.snip) || (nextProps.data.comment != this.state.snip.comment))
this.setState({snip: nextProps.data});
}
handleSnipChange = (ev) => {
let newSnip = {snip: ev.target.value, comment: this.state.snip.comment};
this.setState({snip: newSnip});
Actions.saveSnipEdit(newSnip);
}
handleCommentChange = (ev) => {
let newSnip = {snip: this.state.snip.snip, comment: ev.target.value};
this.setState({snip: newSnip});
Actions.saveSnipEdit(newSnip);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)