简体   繁体   English

如何从中获取道具<route />与反应路由器 dom v6

[英]How to get props from <Route /> with react router dom v6

I am trying to pass :id to url when i click a row of my table trying using navigate("/edit/"+props);当我尝试使用navigate("/edit/"+props);单击我的表的一行时,我试图将:id传递给 url; and onClick={() => handleClick(data.PacienteId)} but it didnt work then used useParams and created handleProceed to use it as onclick={handleProceed} but it still not working i just get url provided by Apiurl but /undefinedonClick={() => handleClick(data.PacienteId)}但它没有工作然后使用useParams并创建handleProceed以将其用作onclick={handleProceed}但它仍然无法正常工作我只是得到 Apiurl 提供的Apiurl/undefined

This is what i have in my routes这就是我的路线

function App() {
  return (
    <>
      <BrowserRouter>
        <Routes>
          <Route path="/" exact element={<Login  />} />
          <Route path="/dashboard" exact element={<Dashboard  />} />
          <Route path="/new" exact element={<Nuevo  />} />
          <Route path="/edit/:id" exact element={<Editar />} />
        </Routes>
      </BrowserRouter>
    </>
  );
}

This is my dashboard where i want to pass id to url clicking on table这是我的仪表板,我想将 id 传递给 url 点击表格

export const Dashboard = (props) => {
  const [paciente, setPaciente] = useState([]);
  const {id}=useParams();
  const navigate = useNavigate();
  useEffect(() => {
    let url = `${Apiurl}pacientes?page=1`;
    axios.get(url).then((response) => {
      setPaciente(response.data);
    });
  }, []);

  const handleClick = (props) => {
    /* navigate("/edit/" + props); */
    navigate(`/edit/${id}`);
  };
  const handleProceed = (e) => {
    /* history.push(`/edit/${id}`); */
    navigate(`/edit/${id}`);
  };
  return (
    <>
      <Header />
      <div className="container">
        <table className="table table-dark table-hover">
          <thead>
            <tr>
              <th scope="col">ID</th>
              <th scope="col">DNI</th>
              <th scope="col">NOMBRE</th>
              <th scope="col">TELEFONO</th>
              <th scope="col">CORREO</th>
            </tr>
          </thead>
          <tbody>
            {paciente.map((data, i) => {
              return (
                <tr key={i} /* onClick={handleProceed} */onClick={() => handleClick(data.PacienteId)}>
                  <td>{data.PacienteId}</td>
                  <td>{data.DNI}</td>
                  <td>{data.Nombre}</td>
                  <td>{data.Telefono}</td>
                  <td>{data.Correo}</td>
                </tr>
              );
            })}
          </tbody>
        </table>
      </div>
    </>
  );
};

In your handle click function you have the parameter named as props, but you've tried to use "id" variable in the navigate method.在您的句柄中单击 function 您有名为 props 的参数,但您尝试在导航方法中使用“id”变量。 Either use props in the navigate method or change the argument name to id.在导航方法中使用道具或将参数名称更改为 id。

 const handleClick = (id) => {
   navigate(`/edit/${id}`);
 };

********* OR ************ ********* 或者 ************

 const handleClick = (props) => {
   navigate(`/edit/${props}`);
 };

try <Route path="/dashboard/:id" element={<Dashboard />} /> with this way you can get id in component DashBoard尝试<Route path="/dashboard/:id" element={<Dashboard />} />这样你就可以在组件 DashBoard 中获取 id

is that what you mean?你是这个意思吗?

i think this may help you.我想这可能会对你有所帮助。 use const {id} = useParams();使用const {id} = useParams();

then然后

const handleProceed = (id) => { /* history.push(``/edit/${id}``); */ navigate(``/edit/${id}``); };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM