簡體   English   中英

如何使用 React js 使表格的整行可點擊<link>或任何其他方式?

[英]How can i make the entire row of a table clickable in react js using <Link> or any other way?

這是代碼

<tbody>
    <tr>
     <td>
      <Link to={{pathname: '/user',
                 state: {
                   number: content.number,
                   name: content.name,
                   age: content.age
                 }
               }}
      />
     {content.accountOwner}
    </td>
    <td>
      {content.address}
   </td>
   <td>{content.profession}</td
  </tr>
</tbody>

此行在 chrome 中變為可點擊,但在 IE 中,因為放在第一列中,只有第一列變為可點擊,而不是整行。 誰能幫我解決這個問題?

您可以使用.push()方法以編程方式導航到該頁面。 如果你使用的是最新的react-router-dom ,你可以使用useHistory鈎子來使用 push 方法。 push()的另一個優點不會破壞您的 UI,因為<Link />組件可能會導致您的 UI 出現問題,因為它是另一個嵌套的元素。

import React from 'react';
import { useHistory } from 'react-router-dom';

export const ExampleComponent = ({ content }) => {
  const history = useHistory();

  function handleRowClick() {
    const { number, name, age } = content;
    history.push('/user', { number, name, age });
  }

  return (
    <tbody>
      <tr onClick={handleRowClick}>
        <td>
          {content.accountOwner}
        </td>
        <td>
          {content.address}
        </td>
        <td>{content.profession}</td>
      </tr>
    </tbody>
  )
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM