2017-06-04 48 views
1

我在textarea中使用defaultValue傳遞this.props.childred,但它給了我錯誤 未捕獲的錯誤:如果您提供了defaultValue,請不要傳遞子對象。如何將this.props.children傳遞到Textarea

什麼其他替代或者是有bassing值

import React, {Component} from 'react'; 

class Update extends Component { 

    constructor(props){ 
     super(props); 
     this.state = { 
      editing:false 
     } 

     this.renderEdit = this.renderEdit.bind(this) 
    } 

    edit(){ 
     this.setState({ 
      editing:true 
     }) 

    } 
    remove(){ 
     alert("remove") 
    } 
    save(){ 
     this.setState({ 
      editing:false 
     }) 
    } 

    renderNormal(){ 
     return (
      <div className="container"> 

     <div className="content"> {this.props.children}</div> 
     <button onClick={this.edit.bind(this)} className="edit" >Edit </button> 
     <button onClick={this.remove} className="remove" >Remove </button> 
     </div> 
     ); 

    } 
    renderEdit(){ 

     console.log('this.props ', this.props) 

console.log('this.props.children ', this.props.children) 

     return (
     <div className="container"> 



     <textarea ref="newText" defaultValue={this.props.children} > </textarea> 
     <button onClick={this.save.bind(this)} className="edit" >Save </button> 
     </div> 
     ); 

    } 

    render(){ 
     var great = "awesome " 

     if(this.state.editing){ 
      return this.renderEdit(); 
     }else{ 
      return this.renderNormal(); 
     } 

    } 
} 
export default Update; 
+0

而不是'this.props.children',一個字符串prop - 'this.props.text'應該可以很好地用於Update組件。 – vijayst

回答

2

使用自閉的語法像這樣的一個更好的方法:

<textarea ref="newText" defaultValue={this.props.children}/> 

或者實際上,你可以刪除空格在<textarea>標籤之間。

+0

謝謝Tharaka它的工作 –