I have a ReactJS project where i get JSON from REST Django host and creating a table with filters for it. I have a Table class :
class MainTable extends React.Component {
constructor(props) {
super(props);
this.state = {
results: []
};
}
componentDidMount(){
axios.get(this.props.link)
.then(res => {
this.setState({results: res.data.results});
});
}
render() {
return (
<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>
);
}
}
And Main where i run all filter classes :
class Main extends React.Component {
constructor() {
super();
this.state = {
origin: '',
limit: 10
};
this.handleChangeOrigin = this.handleChangeOrigin.bind(this);
this.handleChangeLimit = this.handleChangeLimit.bind(this);
}
handleChangeLimit(event) {
this.setState({limit: event.target.value});
}
handleChangeOrigin(event) {
this.setState({origin: event.target.value});
}
render() {
var link = `http://106.120.89.142:8000/database/?limit=${this.state.limit}&`;
if (this.state.origin){
link += `ORIGIN=${this.state.origin.toLowerCase()}`;
console.log(link)
}
return (
<div>
<div>
<h1 className="jumbotron-heading display-4">Here u got database *_*</h1>
</div>
<div>
<Limit handleChange = {this.handleChangeLimit} />
</div>
<div>
<OriginRow handleChange = {this.handleChangeOrigin} />
</div>
<div id="tableWrapper">
<MainTable link={link} />
</div>
</div>
);
}
}
And i have a problem with it, cause when i use componentDidMount()
, my axios.get(my link to REST JSON)
runs only once. When i use axios
in Table class render()
, it hits my server few times per second. Can i make mounting only when my props to Table class changed?