ReactJS组件不使用状态变量呈现textarea

May*_*yas 23 javascript textarea reactjs

我在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>
               );
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您可能已经注意到的那样,它是一个控制器视图,在商店注册,该商店注册到我的AppDispatcher.

正确执行上述步骤.即,当特定的动作triggerd,我的组件被正确使用变量渲染 {this.state.sCurrentBookToShow.title}this.state.sCurrentBookToShow.title先进的日期.

问题来自这部分:

<textarea className="form-control" rows="22" ref="summaryContent" >   
  {this.state.sCurrentBookToShow.summary}
 </textarea>
Run Code Online (Sandbox Code Playgroud)

字符串不会打印在textarea中.

我试过这个调试:

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

summary易变textearea内正确地打印字符串.

请注意,我的浏览器说:

警告:使用defaultValuevalue道具代替设置子项<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>
Run Code Online (Sandbox Code Playgroud)
  • 我换过this.state.sCurrentBookToShow.title通过.summary,以确保梯子不是空的.
  • 我将摘要放入defaultValue道具中

这是输出: 在此输入图像描述

第二次编辑:

我上传了一个突出显示问题的示例应用.我希望这有助于找到一个合适的解决方案

Jyo*_*uri 39

从反应文档中检查此链接:React Textarea Value

基本上对于textArea的反应不支持包含在其中的文本,而您需要将其指定为valuedefaultValue.

正确的方法是

<textarea name="description" value="This is a description." />
Run Code Online (Sandbox Code Playgroud)

要么

<textarea name="description" defaultValue="This is a description." />
Run Code Online (Sandbox Code Playgroud)

与差valuedefaultValue指定defaultValue离开元件不受控制:

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

...同时指定valueReact来控制组件,这意味着您需要更新value属性以确保更改反映在组件中:

由于value属性是在我们的表单元素上设置的,因此显示的值将始终为this.state.value,使React状态成为事实的来源.

要清楚地了解值/默认值之间的差异,请检查以下内容:小提示值默认值区别控制台将始终显示新值但组件不会显示.