2016-11-19 104 views
0

我遇到了一個與更新狀態有關的問題。如何立即爲數組設置狀態?

那麼,我有一些數據,我想用這些數據建立一個表。但是,我想先過濾它。過濾運行良好,但我只有更新過濾的數據並將其投擲到下一個組件...(我知道setState不能立即工作...)

已更新reportReable組件中的報表仍未過濾data ... 什麼是修復它並使用更新數組狀態的最佳方法。

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組件中添加了額外的狀態...現在傳遞的道具是正確的...但是知道我有另一個問題: '未捕獲的NotFoundError:未能在'節點'上執行'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}); 
} 

或者只是打電話給你的方法_filterBy內componentDidMount。

希望這會有所幫助。

+0

我正在考慮這個問題,但我不知道該如何處理'option'參數,我從子組件中獲得... – alingo

+0

您可以創建小提琴嗎? – Boky

+0

對不起,但它的代碼太多了......我不知道如何把相同情況的簡短和正確的例子。 – alingo