2017-07-17 72 views
0

我有父母孫子層次反應,我傳遞值作爲JSON對象在我的code.I有兩個輸入框,從用戶和一個按鈕,它存儲和顯示值onclick的值。在反應中顯示JSON

的爲我的代碼反應代碼:

class Todo extends React.Component{ 
    constructor(props){ 
    super(props); 
    this.state={ 
     input:[], 
     desc:'', 
     expense:'', 
     list:[] 
    } 
    this.save=this.save.bind(this); 
    this.changeDesc=this.changeDesc.bind(this); 
    this.changeExpense=this.changeExpense.bind(this); 
    } 
    changeDesc(e){ 
    this.setState({ 
     desc:e.target.value 
    }) 
    } 

    changeExpense(e){ 
    this.setState({ 
     expense:e.target.value 
    }) 
    } 
    save(saveText){ 
    var list=this.state.list; 
    list.push({ 
     text:saveText,  
    }) 
    this.setState({ 
     list:list, 
     desc:'', 
     expense:'' 
    }) 
    //console.log(input); 
    } 
    render(){ 
     return(
      <div> 
      <Save saveText={this.save} text={this.state.input}/> 
      <Display list={this.state.list}/> 
      </div> 
     ) 
    } 
} 
class Save extends React.Component{ 
    constructor(props){ 
    super(props); 
    this.state={ 
     input:this.props.text 
    } 
    this.save=this.save.bind(this); 
    } 
    save(){ 
    var input=this.state.input; 
    var desc=document.getElementById("desc").value; 
    var expense=document.getElementById("expense").value; 
    input.push({"desc" : desc, "expense": expense}); 
    this.props.saveText({"desc" : desc, "expense": expense}); 
    //console.log(this.props.saveText); 
    this.setState({ 
     input:[] 
    }) 
    } 
    render(){ 
    return(
     <div> 
     <input type='text' id="desc" onChange={this.changeDesc}/> 
     <input type="text" id="expense" onChange={this.changeExpense}/> 
     <input type="button" value="save" onClick={this.save}/> 
     </div> 
    ) 
    } 
} 

class Display extends React.Component{ 
    constructor(props){ 
    super(props); 
    this.state={ 
     todos:[] 
    } 
    } 
    componentWillReceiveProps(nextProps){ 
    this.setState({ 
     todos:nextProps.list 
    }) 
    } 
    render(){ 
    var renderList=this.state.todos; 
    var listElements=[]; 
    var len=Object.keys(renderList).length 
    for(var i=0;i<len;i++){ 
    listElements.push(
     <Post i={i} desc={renderList[i].desc} expense={renderList[i].expense}/> 
    ); 
    //console.log(listElements); 

    } 
    return (
     <div> 
     { 
     listElements 
     } 
     </div> 
    ); 
    } 
} 

class Post extends React.Component{ 
    render(){ 
    return(
     <div> 
     <span>{this.props.desc}</span> 
     <span>{this.props.expense}</span> 
     <span>Edit</span> 
     </div> 
    ) 
    } 
} 

ReactDOM.render(<Todo/>,document.getElementById('root')); 

當我執行這個代碼,我得到的輸出this而且還有在輸出中沒有錯誤。上述代碼的邏輯有什麼問題。

回答

1

在todos組件中,您正在嘗試將其列爲list.push({text:saveText}),因此renderList[i].desc, renderList[i].expenseundefined。無論是將其更改爲renderList[i].text.desc, renderList[i].text.expense

或改變list.pushlist.push(saveText)這裏:

save(saveText){ 
    var list=this.state.list; 
    list.push(saveText) 
    this.setState({ 
     list:list, 
     desc:'', 
     expense:'' 
    }) 
    //console.log(input); 
    } 

Working codepen

+0

,但是當我這樣做,list.push({文字:SAVETEXT}),然後我也假設我正在做同樣的事情,這有什麼不同。你能解釋一下 – Aayushi

+1

好吧,所以在saveText中你得到一個對象,你用'list.push({text:saveText})做什麼'將對象分配給另一個對象的鍵列表。但是當你做'list.push(saveText)'時,你基本上是把對象推到數組中,我希望我能夠解釋它 –