<div id="root"></div>
@import url("https://fonts.googleapis.com/css2?family=Quicksand:wght@500&display=swap");
body {
font-family: "Quicksand", sans-serif;
display: flex;
justify-content: center;
padding: 0;
color: #4d4d4d;
}
#title {
text-align: center;
}
#employee {
border-collapse: collapse;
border: 3px solid #ddd;
}
#employee td,
#employee th {
border: 1px solid #ddd;
padding: 12px;
}
#employee tr:hover {
background-color: #ddd;
}
#employee th {
padding: 10px;
text-align: center;
background-color: #4caf50;
color: white;
}
.operation {
text-align: center;
}
.button {
border: none;
outline: none;
font-size: 11px;
font-family: "Quicksand", sans-serif;
color: #f44336;
padding: 3px 10px;
border-radius: 8px;
cursor: pointer;
border: 1px solid #f44336;
background-color: transparent;
}
.button:active {
border: 1px solid blue;
}
const URL = 'https://jsonplaceholder.typicode.com/users'
function useAPi(url) {
const [data, setData] = React.useState([])
React.useEffect(() => {
getData()
}, [])
const getData = async () => {
const response = await axios.get(url)
setData(response.data)
}
const removeData = (id) => {
axios.delete(`${url}/${id}`).then(() => {
const del = data.filter((item) => id !== item.id)
setData(del)
})
}
return { data, removeData }
}
const Table = () => {
const { data, removeData } = useAPi(URL)
const renderHeader = () => {
let headerElement = ['id', 'name', 'email', 'phone', 'operation']
return headerElement.map((key, index) => {
return <th key={index}>{key.toUpperCase()}</th>
})
}
const renderBody = () => {
return data && data.map(({ id, name, email, phone }) => {
return (
<tr key={id}>
<td>{id}</td>
<td>{name}</td>
<td>{email}</td>
<td>{phone}</td>
<td className='operation'>
<button className='button' onClick={() => removeData(id)}>Delete</button>
</td>
</tr>
)
})
}
return (
<>
<h1 id='title'>React Table</h1>
<table id='employee'>
<thead>
<tr>{renderHeader()}</tr>
</thead>
<tbody>
{renderBody()}
</tbody>
</table>
</>
)
}
ReactDOM.render(<Table />, document.getElementById('root'));
View Compiled
This Pen doesn't use any external CSS resources.