Using React Router's <Link /> to wrap a table row #2336
|
Hi there, Thank you for such a great library. I am running into a strange issue when I wrap a row with the component from react router. I have tried to see how to do this but I can't find something. I apologise if I missed something. Thank you for any help :) Before: After: <tr {...row.getRowProps()} className="cursor-pointer hover:bg-gray-50">
<Link to={`/use-cases/${row.original.id}`}>
{row.cells.map(cell => {
if (cell.column.Header === 'Level') {
return (
<td {...cell.getCellProps()} className="px-6">
<Badge text={useCaseLevels[cell.value]} />
</td>
)
} else if (cell.column.Header === 'Epics') {
return (
<td {...cell.getCellProps()} className="px-6">
{cell.value.map(v => <Badge text={v.epic.name} key={v.epic.name} />)}
</td>
)
} else {
return (
<td
{...cell.getCellProps()}
className={`${cell.column.Header === 'Name' ? 'text-indigo-600 font-medium' : 'text-gray-500'} px-6 py-4 whitespace-no-wrap text-sm leading-5`}
>
{cell.render('Cell')}
</td>
)
}
})}
</Link>
</tr> |
Replies: 8 comments 12 replies
|
Maybe try giving the Link 100% width? It's hard to say without a working example |
|
Unfortunately you can't do this. This is not react-table's issue. But as I can you want link the whole row. You can use the onClick prop on import { useHistory } from "react-router-dom";
const Table = () => {
...
const history = useHistory();
const handleRowClick = (row) => {
history.push(`/use-cases/${row.original.id}`);
}
return (
...
<tr onClick={()=> handleRowClick(row)}}>
...
</tr>
...
);
} |
|
GuptaSiddhant thanks for your solution. In case anyone is trying to enable a cmd/ctrl click to open in a new tab Checking the event for a cmd/ctrl click worked for me. <tr
key={row.id}
className={styles.row}
{...row.getRowProps()}
onClick={(event) =>{
if (event.metaKey || event.ctrlKey){
const win = window.open( `/audio-content/${row.original.id}`, "_blank");
win?.focus();
}
else {
history.push(
`/audio-content/${row.original.id}`,
)
}
}
}> |
|
|
After more thought on this subject, I’m pretty sure my first recommendation would be to not make the entire row a clickable link. Instead, I would make one or two main elements of the row like the first two cells clickable links and make it obvious that they are clickable. Only after considering that, would I recommend conditionally rendering special markup and event handlers for links. |
|
This helped me: stackoverflow.com/a/62451524/15172167 |
|
You shouldn't wrap the |







Unfortunately you can't do this. This is not react-table's issue.
This is an HTML issue which requires
tdto be direct children oftr. All other elements likedivorLinkshould be insidetd.But as I can you want link the whole row. You can use the onClick prop on
trto achieve the same result. The catch is you have to use history api to manipulate your browser.