2017-07-10 5 views
1

私のReactJSプロジェクトでは、私はTableを持っており、それをフィルタリングしています。私はaxiousDjango RESTホストから使用してJSONファイルを得ました。だから私は私のメインTableのためのクラスがあります。ShouldComponentUpdate()を使用している間に1つのステップ遅延

class MainTable extends React.Component { 

    constructor(props) { 
    super(props); 
    this.state = { 
     results: [], 
     count: 0 
    }; 
    } 

    shouldComponentUpdate(nextProps) { 
    console.log(this.props.link + " : " + nextProps.link); 
    return JSON.stringify(this.props.link) !== JSON.stringify(nextProps.link); 
    } 

    render() { 
    axios.get(this.props.link) 
     .then(res => { 
     this.setState({results: res.data.results}); 
     this.setState({count: res.data.count}); 
    }); 
    return (
     <div> 
     <div> 
      <Label> We got {this.state.count} elements in our database. </Label> 
     </div> 
     <div> 
      <Table hover striped bordered responsive size="sm"> 
      <thead> 
       <tr> 
       <th>Name</th> 
       <th>Name</th> 
       </tr> 
      </thead> 
      <tbody> 
       {this.state.results.map(result => 
       <tr key={result.fileId}> 
        <td>{result.name}</td> 
        <td>{result.name}</td> 
       </tr> 
      )} 
      </tbody> 
      </Table> 
     </div> 
     </div> 
    ); 
    } 
} 

をしかし、私は私のアプリを実行すると、私はここで、その中に一歩遅れを、得たログは次のとおりです。

"http://106.120.89.142:8000/database/?limit=10& : http://106.120.89.142:8000/database/?limit=10&" bundle.js:53418:7 
./src/App.js 
"http://106.120.89.142:8000/database/?limit=10& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcrsutc&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcrsutc& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcrsutc&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcrsutc& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltcall& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3&" bundle.js:53418:7 
"http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3& : http://106.120.89.142:8000/database/?limit=10&ORIGIN=dcseltc3&" 

それを2回再放送データチェックを行い私のレンダリングは次のチェックの後でしかない。多分私は何か間違ったことをしました。私はReactJSで新しく、私はそれが簡単に答えるべきだと思います。

+3

レンダリングの途中で状態を設定するべきではありません。 アクシオ要求はアクション内にある必要があり、データはRedux/mobx/fluxまたはあなたが選択した状態マネージャを使用して入力する必要があります。 したくない場合は、componentWillMount/componentDidMountでデータを取得し、その状態を設定することができます。 –

+1

状態マネージャがなくても、componentDidMountステップでapi呼び出しと新しいsetStateを行う必要があります。これは、コンポーネントのWillMountで悪い習慣と考えられています:https://facebook.github.io/react/docs/react-component.html#componentdidmount – Nevosis

+0

私は 'shouldComponentUpdate'を使わずに' render() 'に' axios'を入れます。私のホストに毎秒数回ヒットしたので、私はそれを変更したいと思っていました。私が 'componentDidMount()'を使うと、私が新しい小道具を送った後、私の 'MainTable'は新しいデータで更新されません。 –

答えて

0

レンダリングは常に新しい状態インスタンスが設定された後に呼び出されるため、setStateをcomponentDidMount()に配置すると、新しいデータで更新されるはずです。そしてsetStateは一度だけです。

class MainTable extends React.Component { 
... 
componentDidMount() { 
    axios.get(this.props.link) 
     .then(res => { 
     this.setState({results: res.data.results, count: res.data.count}); 
    }); 
} 
componentWillReceiveProps(nextProps) { 
    axios.get(nextProps.link) 
     .then(res => { 
     this.setState({results: res.data.results, count: res.data.count}); 
    }); 
} 
... 
render() { 
return ... 
} 
+1

しかし、テーブルは一度だけロードされます。私がフィルターを使ってリンクを変更すると、私の 'テーブル'は同じ開始データで読み込まれます。私のメインでは ''だけを使用し、リンクは 'REST'データを要求するためにフィルタで変更されるオブジェクトです。 –

+1

あなたは本当にあなたのポストでそれを言及しなかった、componentWillReceivePropsを見て、新しい小道具がコンポーネントに渡されるたびに呼び出され、私は私の投稿を編集します:https://facebook.github.io/反応する/ docs/react-component.html#コンポーネントは受信します – Nevosis

+0

申し訳ありませんが、今後の記事でより正確になります。答えのためのTy。 –

関連する問題