2016-11-19 12 views
0

私は反応の状態を更新することに関連した1つの問題に遭遇しました。すぐに配列の状態を設定するには?

私はいくつかのデータを持っており、このデータを使ってテーブルを作成したいと考えています。しかし、私は最初にそれをフィルタリングしたいと思います。フィルタリングは正常に機能していますが、フィルタリングされたデータを更新して次のコンポーネントにスローするだけで問題はありません。(私はsetStateがすぐには動作しないことを知っています...)

ReportTableコンポーネントのupdatedReportsはまだフィルタリングされていませんデータ... それを修正し、配列の状態を更新する最も良い方法は何ですか?あなたは多分コンポーネントのライフサイクルメソッドを使用することができます

export default class ReportContent extends React.Component { 

constructor(props) { 
    super(props); 
    this.state = { 
     currentReports: this.props.reports 
    }; 
} 

_filterBy(option) { 
    let updatedReports = [...this.props.reports].filter(report => { 
     if (report.organizations === option || report.reportType === option) { 
      return report; 
     } 
    }); 
    console.log(updatedReports); 
    this.setState({currentReports: updatedReports}); 
} 

render() { 
    return (
     <div className="reports-table"> 
      <ReportMenu organizations={this.props.organizations} reportTypes={this.props.reportTypes} 
         filterBy={this._filterBy.bind(this)}/> 
      <ReportTable updatedReports={this.state.currentReports}/> 
     </div> 
    ); 
} 

}

+0

'_filterBy'メソッドの' option'パラメータの構造を除いて、私はコードに何か間違いはありません。 'ReportMenu'コンポーネントと' option'パラメータの構造体を投稿できますか? – jpdelatorre

+0

@jpdelatorreオプションのパラメータは正常です...私がupdatedReportsをログに記録するとき - 私は欲しいものを得ました...問題は、currentReportsがすぐに更新されないということです。 – alingo

答えて

0

提供したコードに問題はありません。 setStateは、正しく呼び出された場合に動作するはずです。問題はあなたの他のコンポーネントやデータの中にあるということです。

コードを使用するスニペットです。他のコンポーネントをどのように実装するのか分かりませんので、ここでいくつかの前提を立てました。

class ReportMenu extends React.Component { 
    render() { 
     return <div className="well well-default"> 
      <select className="form-control" onChange={(e) => { 
       this.props.filterBy(e.target.value) 
      }}> 
       <option> - </option> 
       {this.props.organizations.map(
        (item, index) => <option key={index}>{item}</option> 
       )} 
      </select> 
      <br/> 
      <select className="form-control" onChange={(e) => { 
       this.props.filterBy(e.target.value) 
      }}> 
       <option> - </option> 
       {this.props.reportTypes.map(
        (item, index) => <option key={index}>{item}</option> 
       )} 
      </select> 
     </div> 
    } 
} 

class ReportTable extends React.Component { 
    render() { 
     return <table className="table table-bordered"> 
      <tbody> 
       {this.props.updatedReports.map(
        (item, index) => <tr key={index}> 
         <td>{index}</td> 
         <td>{item.organizations}</td> 
         <td>{item.reportType}</td> 
        </tr> 
       )} 
      </tbody> 
     </table> 
    } 
} 

class ReportContent extends React.Component { 
    constructor(props) { 
     super(props); 
     this.state = { 
      currentReports: this.props.reports 
     }; 
    } 
    _filterBy(option) { 
     let updatedReports = this.props.reports.filter(report => { 
      if (report.organizations === option || report.reportType === option) { 
       return report; 
      } 
     }); 
     console.log(updatedReports); 
     this.setState({currentReports: updatedReports}); 
    } 
    render() { 
     return (
      <div className="reports-table"> 
       <ReportMenu 
        organizations={this.props.organizations} 
        reportTypes={this.props.reportTypes} 
        filterBy={this._filterBy.bind(this)}/> 
       <ReportTable updatedReports={this.state.currentReports}/> 
      </div> 
     ); 
    } 
} 

class App extends React.Component { 
    render() { 
     const reports = [ 
      { 
       organizations: 'orgA', 
       reportType: 'typeA' 
      }, { 
       organizations: 'orgA', 
       reportType: 'typeB' 
      }, { 
       organizations: 'orgB', 
       reportType: 'typeA' 
      }, { 
       organizations: 'orgB', 
       reportType: 'typeB' 
      } 
     ]; 
     return <div className="container"> 
      <h1 className="page-header">Reports</h1> 
      <ReportContent 
       reports={reports} 
       organizations={['orgA', 'orgB']} 
       reportTypes={['typeA', 'typeB']}/> 
     </div> 
    } 
} 

ReactDOM.render(<App/>, document.getElementById('root')); 
+0

あなたは正しいです!私はReportTableコンポーネントの余分な状態を持っています...今は通過する小道具は正しいです...しかし、私は別の問題があることを知っています: 'Uncaught NotFoundError: 'node'で 'removeChild'を実行できませんでした:削除されたノードはこのノードの子ではありません。 ' – alingo

+0

この問題は、最初の質問とは全く別のものです。この質問に回答したことを記し、詳細を記した別の記事を投稿することをお勧めします。 – jpdelatorre

1

Here is more info.

componentDidMountはすべきことです。

componentDidMount(){ 
    let updatedReports = [...this.props.reports].filter(report => { 
     if (report.organizations === option || report.reportType === option) { 
      return report; 
     } 
    }); 
    this.setState({currentReports: updatedReports}); 
} 

それともcomponentDidMount中にあなたの方法_filterByを呼び出す:あなたはこのような何かを行うことができます。

これが役に立ちます。

+0

私はそれについて考えていましたが、私は子供のコンポーネントから取得する 'オプション'パラメータで何をするのかわかりません... – alingo

+0

フィドルを作成できますか? – Boky

+0

申し訳ありませんが、あまりにも多くのコードがあります...そして、私は同じ状況の短く正しい例を入れる方法を正確にはわかりません。 – alingo

関連する問題