gpt4 book ai didi

javascript - ReactJS 组件不渲染带有状态变量的文本区域

转载 作者:行者123 更新时间:2023-12-03 13:02:37 26 4
gpt4 key购买 nike

我在 React 中遇到了一个有趣的错误。

我有这个组件:

'use strict';
import SummaryStore from '../stores/SummaryStore';
import React from 'react';

export default class ChangeSummaryForm extends React.Component {
constructor() {
// store initialisation
SummaryStore.register();

var vRating = SummaryStore.getBookForSummaryPrint().summaryRating;
var vStarClassName = this.getRatingClasses(vRating);
this.state = {
sStarClassName: vStarClassName,
sCurrentBookToShow: SummaryStore.getBookForSummaryPrint()
};

this.thereIsASummaryToShow = this.thereIsASummaryToShow.bind(this);
}

getRatingClasses(pRating) {
var vI, vStarClassName = [];
for(vI = 0; vI < 4; vI++) {
if(pRating > 0) {
vStarClassName.push("glyphicon glyphicon-star");
pRating--;
} else {
vStarClassName.push("glyphicon glyphicon-star-empty");
}
}
return vStarClassName;
}
componentDidMount() {
SummaryStore.addChangeListener(this.thereIsASummaryToShow);
}

componentWillUnmount() {
SummaryStore.removeChangeListener(this.thereIsASummaryToShow);
}

thereIsASummaryToShow() {

this.setState({sCurrentBookToShow: SummaryStore.getBookForSummaryPrint(),
sStarClassName: this.getRatingClasses(SummaryStore.getBookForSummaryPrint().rating)
});
$("#summaryModal").modal('show');
}
render() {
return (<div className="modal fade" id="summaryModal">
<form>
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<button type="button" className="close" data-dismiss="modal" ariaLabel="Close"><span ariaHidden="true">&times; </span> </button>
<div style={{color: 'black'}}>
{this.state.sStarClassName.map(function(pCurrentClassName) { return (<span className={pCurrentClassName}></span>
);
})}
<h4 className="modal-title">Summary of {this.state.sCurrentBookToShow.title}</h4>
</div>
</div>
<div className="modal-body">

<div className="form-group">
<textarea className="form-control" rows="22" ref="summaryContent" >{this.state.sCurrentBookToShow.summary}</textarea>
</div>

</div>
<div className="modal-footer">
<button type="button" className="btn btn-default" data-dismiss="modal" >Close</button>
<input type="submit" className="btn btn-primary" value="Save"/>
</div>
</div>
</div>
</form>
</div>
);
}
}

您可能会注意到,它是 controller-view在注册到我的 AppDispatcher 的商店中监听。

以上步骤已正确执行。即,当触发特定操作时,我的组件将使用变量 {this.state.sCurrentBookToShow.title} 正确呈现。和this.state.sCurrentBookToShow.title最新的。

问题来自这部分:

<textarea className="form-control" rows="22" ref="summaryContent" >   
{this.state.sCurrentBookToShow.summary}
</textarea>

该字符串未打印在文本区域中。

我尝试用这个来调试:

    render() {
var summary = "this is a summary";
return (// .. shortened for brevity
<textarea className="form-control" rows="22" ref="summaryContent">
{summary}
</textarea> ..);
}

summary在可变文本区域内正确打印的字符串。

请注意,我的浏览器显示:

Warning: Use the defaultValue or value props instead of settingchildren on <textarea>.

但我稍后会修复这个问题,因为我认为它对当前问题没有影响。

编辑:我考虑了你的评论(到目前为止),所以我更新了我的代码,如下所示:

                <h4 className="modal-title">Summary of {this.state.sCurrentBookToShow.summary}</h4>
</div>
</div>
<div className="modal-body">

<div className="form-group">
{this.state.sCurrentBookToShow.summary}
<textarea className="form-control" rows="22" ref="summaryContent" defaultValue={this.state.sCurrentBookToShow.summary}></textarea>
</div>
  • 我替换了this.state.sCurrentBookToShow.title通过.summary使确保梯子不是空的。
  • 我将摘要放入 defaultValue Prop

这是输出: enter image description here

第二次编辑:

我上传了一个示例app这凸显了这个问题。我希望这有助于找到合适的解决方案

最佳答案

检查 react 文档中的此链接:React Textarea Value

基本上,对于textArea,react 不支持其中包含的文本,您需要将其指定为 value defaultValue

正确的方法是

<textarea name="description" value="This is a description." />

<textarea name="description" defaultValue="This is a description." />

value的区别和defaultValuespecifying defaultValue leaves the component uncontrolled :

With an uncontrolled component, you often want React to specify the initial value, but leave subsequent updates uncontrolled. To handle this case, you can specify a defaultValue attribute instead of value.

...同时specifying value instructs React to control the component ,这意味着您需要更新 value 属性以确保更改反射(reflect)在组件中:

Since the value attribute is set on our form element, the displayed value will always be this.state.value, making the React state the source of truth.

要清楚地了解值/默认值之间的差异,请检查:Fiddle for value Default Value Distinction 控制台将始终显示新值,但组件不会。

关于javascript - ReactJS 组件不渲染带有状态变量的文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30730369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com