Recently I started working on React js16 with Typescript. Moving from angular to react. I am trying to create my own sortable table. Here i am using native JS like code. And I am not sure wether I am doing right or wrong. Cause in angular we were not using native js. In react we can use ref but as per facebook it is having some issues. Another approach like ReactDOM.findNode which is also deprecated i think. So what would be the best approach to do it or whatever I am doing that is fine? I am struggling to find the best approach to do it.
Please see the code written inside showDelete() function. The way i am adding the className and replacing the class name those are correct or any other approach recommended? Sorting logic is not there cause I will do server side sorting.
class CoursePage extends React.Component<CoursePageProps, any> {
constructor(props: CoursePageProps) {
super(props);
this.showDelete = this.showDelete.bind(this);
}
showDelete(event: any) {
let d = document.querySelectorAll('.customTable th');
let c = document.getElementsByClassName('sortArrow') as HTMLCollectionOf<HTMLElement>;
for (let i = 0; i < c.length; i++) {
c[i].style.display = 'none';
}
for (let i = 0; i < d.length; i++) {
d[i].className = d[i].className.replace(' active', '');
}
event.currentTarget.className += ' active';
event.currentTarget.childNodes[1].className += ' fa fa-long-arrow-down';
event.currentTarget.childNodes[1].style.display = 'inline';
}
render() {
return (
<div>
<Custom courses={this.props.courses} showDelete={this.showDelete}/>
</div>
);
}
}
function mapStateToProps(state: any) {
return {
courses: state.courses,
};
}export default connect(mapStateToProps)(CoursePage);
export default class Custom extends React.Component<buttonProps, any> {
render() {
const {showDelete, courses} = this.props;
return (
<div>
<table className="table customTable">
<thead>
<tr>
<th onClick={(e) => showDelete(e)}>Id<i className="sortArrow"/></th>
<th onClick={(e) => showDelete(e)}>Name<i className="sortArrow"/></th>
</tr>
</thead>
<tbody>
...
</tbody>
</table>
</div>
);
}
}