私のReactJSプロジェクトでは、私はTable
を持っており、それをフィルタリングしています。私はaxious
をDjango 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
で新しく、私はそれが簡単に答えるべきだと思います。
レンダリングの途中で状態を設定するべきではありません。 アクシオ要求はアクション内にある必要があり、データはRedux/mobx/fluxまたはあなたが選択した状態マネージャを使用して入力する必要があります。 したくない場合は、componentWillMount/componentDidMountでデータを取得し、その状態を設定することができます。 –
状態マネージャがなくても、componentDidMountステップでapi呼び出しと新しいsetStateを行う必要があります。これは、コンポーネントのWillMountで悪い習慣と考えられています:https://facebook.github.io/react/docs/react-component.html#componentdidmount – Nevosis
私は 'shouldComponentUpdate'を使わずに' render() 'に' axios'を入れます。私のホストに毎秒数回ヒットしたので、私はそれを変更したいと思っていました。私が 'componentDidMount()'を使うと、私が新しい小道具を送った後、私の 'MainTable'は新しいデータで更新されません。 –