带有值的 React textarea 是只读的,但需要更新 [英] React textarea with value is readonly but need to be updated
问题描述
我的 React 应用中有一个 textarea 填充了一个值.我希望更新此 textarea 并提交表单以更新数据库中的行.
描述变量用数据库中的值填充文本区域.该字段未禁用.
我尝试附加一个 onChange 事件,该事件调度一个动作 (redux) 来更改 textarea 的值,但它为每个字母触发,而且速度太慢.
如何创建一个由值填充且可编辑的文本区域?
谢谢!
这是一个带有两个文本区域的组件.props 和 state 之间存在受控关系.这是输入的关键.注意 componentWillReceiveProps.
import React, {Component} from 'react';从'../../flux/Actions'导入动作;让 CurrentSnipDivSty = {高度:'calc(((100% - 40px) * .4) - 3px)',最小高度:'9.5em',宽度:'100%'};让 CurrentSnipTextAreaSty = {背景颜色:'#213919',边框:'1px 实心 #314929',颜色:'#afac87',字体大小:'1em',高度:'计算(50% - 20px)',溢出:'自动',填充:'5px',宽度:'计算(100% - 12px)'};类 SnipsDetailRender 扩展组件 {使成为() {让 snip = this.state.snip.snip;让评论 = this.state.snip.comment;返回 (<div id='CurrentSnipDivSty' style={CurrentSnipDivSty}><文本区域值={snip}onChange={this.handleSnipChange}style={CurrentSnipTextAreaSty}/><文本区域价值={评论}onChange={this.handleCommentChange}style={CurrentSnipTextAreaSty}/>
);}}导出默认类 SnipsDetail 扩展 SnipsDetailRender {构造函数(){极好的();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) =>{让 newSnip = {snip: ev.target.value, comment: this.state.snip.comment};this.setState({snip: newSnip});Actions.saveSnipEdit(newSnip);}handleCommentChange = (ev) =>{让 newSnip = {snip: this.state.snip.snip, comment: ev.target.value};this.setState({snip: newSnip});Actions.saveSnipEdit(newSnip);}}
I have a textarea in my React app who is filled with a value. I want this textarea to be updated and the form submited to update the row in the database.
<textarea id="description" className="materialize-textarea" length="120" value={description} disabled={isDisabled}></textarea>
The description variable fill the textarea with the value from the database. The field is not disabled.
I tried to attach an onChange event who dispatch an action (redux) to change the value of the textarea, but it trigger for each letter and it's too slow.
How can i create a textarea who is filled by a value and is editable?
Thank you!
Here is a component with two textareas. There is a controlled relation between props and state. That is the key for inputs. Note 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);
}
}
这篇关于带有值的 React textarea 是只读的,但需要更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!