我在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>
               );
    }
}

正如你可能注意到的,在我的AppDispatcher上注册的store 里听音乐是controller-view美元.

正确执行上述步骤.i、 e,当特定动作被触发时,我的组件被正确呈现,变量{this.state.sCurrentBookToShow.title}this.state.sCurrentBookToShow.title是最新的.

The problem来自这一部分:

<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> ..);
    }

在可变文本ea中正确打印summary字符串.

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

警告:使用defaultValuevalueprops ,而不是设置

但我稍后会解决这个问题,因为我认为它对当前的问题没有影响.

EDIT:

                <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
  • 我把摘要写进了defaultValueprops 里

Here is the output: enter image description here

第二次编辑:

我上传了一个样本app,突出了这个问题.我希望这将有助于找到适当的解决办法

推荐答案

从react docs:React Textarea Value查看此链接

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

因此,正确的方法是

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

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

The difference with value and defaultValue is that specifying defaultValue leaves the component uncontrolled:

对于不受控制的组件,您通常希望React指定初始值,但让后续更新不受控制.要处理这种情况,可以指定defaultValue属性而不是value.

...while specifying value instructs React to control the component, meaning you need to update value property to make sure that change is reflected in the component:

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

To get a clear idea of difference between value / default value check this: Fiddle f或 value Default Value Distinction Console will always show new value but component will not.

Reactjs相关问答推荐

避风港访问端口以包含Reaction应用程序

如何在单击行中的图标时避免选中ionic 复选框?

Reaction Google Maps API无法获取标题

当项目开始时,它不会从三元运算符(REACT)加载一些tailwind 类名称

为什么我得不到我想要的数据?

如何使用TouchStart/TouchEnd在Table ReactJS中交换位置?

透明MOV文件未出现在我的React网页中

错误:操作必须是普通对象.使用自定义中间件进行异步操作. Redux/工具包

使用 MUI 菜单时添加新 html 元素时布局意外滚动跳转

如何将 MUI X 数据网格单元格方向更改为行级而不是列级?

useMemo 依赖项的行为

React - 函数组件 - 点击两次问题处理

try 从 React JS 构建此教程游戏但无法继续前进

损坏的图像 React useState

如何在 React Native 中更新跨屏幕呈现的上下文状态变量?

null 不是对象(判断RNSound.IsAndroid)

React Router 6.4CreateBrowserRouter子元素不显示

如何在 React 中制作动画?

警告:您在没有 `onChange` 处理程序的情况下为表单字段提供了 `value` 属性

有没有办法只针对 React map 中的一个按钮?