Example of how to use table-sort.js in a ReactJS application.
Download with NPM:
npm install table-sort-js
import React, { Component } from "react";
import tableSort from "table-sort-js/table-sort.js";
export class App extends Component {
componentDidMount() {
tableSort()
}
render(){
return(
<div>
<table className="table-sort">
<thead>
<tr>
<th>Alphabet</th>
<th className="order-by-desc">Order</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alpha</td>
<td>1</td>
</tr>
<tr>
<td>Bravo</td>
<td>2</td>
</tr>
<tr>
<td>Charlie</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
);
}
}
export default App;
import React, { useEffect,useState,} from "react";
import tableSort from "table-sort-js/table-sort.js";
function App(){
useEffect(() => {
tableSort()
},[]);
return(
<div>
<table className="table-sort">
<thead>
<tr>
<th>Alphabet</th>
<th className="order-by-desc">Order</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alpha</td>
<td>1</td>
</tr>
<tr>
<td>Bravo</td>
<td>2</td>
</tr>
<tr>
<td>Charlie</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
);
}
export default App;
import React, { useEffect, useState } from "react";
function App() {
useEffect(() => {
(async () => {
const tableSortJs = await import("table-sort-js/table-sort.js");
return tableSortJs;
})().then((tableSortJs) => {
tableSortJs.default();
});
}, []);
return(
<div>
<table className="table-sort">
<thead>
<tr>
<th>Alphabet</th>
<th className="order-by-desc">Order</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alpha</td>
<td>1</td>
</tr>
<tr>
<td>Bravo</td>
<td>2</td>
</tr>
<tr>
<td>Charlie</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
);
}
export default App;
Option 2: Download table-sort-js from
table-sort.js
(Select option save as.)
import React, { Component } from "react";
export class App extends Component {
componentDidMount() {
const script = document.createElement("script");
script.src = "table-sort.js";
script.async = true;
document.body.appendChild(script);
}
render(){
return(
<div>
<table className="table-sort">
<thead>
<tr>
<th>Alphabet</th>
<th className="order-by-desc">Order</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alpha</td>
<td>1</td>
</tr>
<tr>
<td>Bravo</td>
<td>2</td>
</tr>
<tr>
<td>Charlie</td>
<td>3</td>
</tr>
</tbody>
</table>
</div>
);
}
}
export default App;