2016-10-25 11 views
-1

以下のコードで何が問題なのかよく知っているかもしれませんか?私は<TextNote>タグのループで何かが間違っていると思いますが、エラーメッセージがないので何が間違っているかはわかりません。ありがとうReactJSのレンダリング機能のループ

EDIT FYI、ボタンのみがレンダリングされ、TextNoteはまったくレンダリングされません。 TextNoteは別のJSファイルで、完全に動作します。

render() { 
return (
    <div className="textBoard"> 
    {this.state.textNotes.forEach((note) => { 
     return (<TextNote key={note.id} 
        id={note.id} 
        onChange={this.update} 
        onRemove={this.remove}> 
       {note.note} 
     </TextNote>) 
    } 
    )} 
    <button onClick={() => this.add('New Text')}>ADD</button> 
    </div> 
)} 
+1

こんにちはビクターを助けることを願って、map代わりのforEachを使用することをお勧め - あなたが取得している結果とは何ですか、あなたが期待している結果は何ですか? –

+0

「私は何かが間違っていると思う」---それはなぜそれだと思いますか? – zerkms

+5

"TextNoteはレンダリングされません" ---それは 'Array.prototype.forEach'が何も返さないため、代わりに' Array.prototype.map'が必要だったからです。 – zerkms

答えて

0

forEachreturnを負いません。代わりにmapを使用してください。

render() { 
return (
    <div className="textBoard"> 
    {this.state.textNotes.map((note) => { 
     return (<TextNote key={note.id} 
        id={note.id} 
        onChange={this.update} 
        onRemove={this.remove}> 
       {note.note} 
     </TextNote>) 
    } 
    )} 
    <button onClick={() => this.add('New Text')}>ADD</button> 
    </div> 
)} 
0

あなたはそれがまたforEach方法で作業が、少し異なる方法で得ることができます。

render(){ 
    const items = []; 
    this.state.textNotes.forEach((note) => { 
    items.push(<TextNote // instead of return push it in an array 
       key={note.id} 
       id={note.id} 
       onChange={this.update} 
       onRemove={this.remove}> 
       {note.note} 
       </TextNote> 
    }) 
    return <div> 
    {items} 
    </div> 
} 

私もそれはあなた

関連する問題