I have this component class (and some related methods):
const sortData = (name, data) => {
return data.sort((a, b) => {return b[name] - a[name]});
};
class LeaderTable extends React.Component {
renderByRecent() {
let data = sortData('recent', this.props.list);
ReactDOM.render(
<LeaderTable list={data}/>,
document.getElementById('board'))
}
render() {
return (
<table className="table table-bordered">
<thead>
<tr>
<td><strong>#</strong></td>
<td><strong>Camper Name</strong></td>
<td id="recent" onClick={() => this.renderByRecent()} className="text-center">Points in past 30 days</td>
<td id="alltime" onClick={() => this.render()} className="text-center">All time points</td>
</tr>
</thead>
<tbody>
{this.props.list.map(function(item, index) {
let url = "https://www.freecodecamp.com/" + item.username;
return (
<tr key={index}>
<td>{index}</td>
<td>
<a href={url}>
<img src={item.img} className="logo"/> {item.username}
</a>
</td>
<td className="text-center">{item.recent}</td>
<td className="text-center">{item.alltime}</td>
</tr>
);
})}
</tbody>
</table>
);
}
}
Now first render happens when page loads. It is simply called in javascript file like this:
ReactDOM.render(
<LeaderTable list={campersData}/>,
document.getElementById('board'))
This one is working fine.
What I need now is to rerender same component, but with different data (actually just with different order of that data).
If you look into renderByRecent
method, here I "rerender" by passing same data sorted in different way (and is is called using onClick
on td
with id='"recent"
. But I do not know if this is really a good pattern to rerender.
Also I want to rerender back original data if td
with id="alltime"
is clicked. This part seems to not work (It does call render
method every time I press respective td
, but nothing changes). I guess I can't recal render
method and hope to rerender
it?
What kind of pattern is usually done with react if you have some similar case like this?
Update
Posted my original code on codepen, for easier investigation: https://codepen.io/andriusl/pen/YxWXzg