简体   繁体   English

React.js,如何获取 URL 路径名中的属性?

[英]React.js, How to fetch the properties in a URL pathname?

I'm trying to fetch the uid and token through the URL pathname using match in my React app.我正在尝试在我的 React 应用程序中使用match通过 URL 路径名获取 uid 和令牌。 But I got error:但是我得到了错误:

Uncaught TypeError: n is undefined未捕获的类型错误:n 未定义

and this is my code snippet:这是我的代码片段:

const ResetPasswordConfirm = ( {match, reset_password_confirm} ) => {
    ...
    const onSubmit = e=> {
    e.preventDefault();

    const uid = match.params.uid;
    const token = match.params.token;

    reset_password_confirm(uid, token, new_password, re_new_password);
    setRequestSent(true);
    };
};

According to this thread: https://stackoverflow.com/questions/70609785/uncaught-typeerror-match-is-undefined#:~:text=react%2Drouter%2Ddom%20v5,render%20or%20children%20prop%20functions .根据这个线程: https://stackoverflow.com/questions/70609785/uncaught-typeerror-match-is-undefined#:~:text=react%2Drouter%2Ddom%20v5,render%20or%20children%20prop%20functions It says it might have something to do with the version of react router, so I tried to use useParams(), but I found it hard to implement it...说是react router的版本可能有关系,所以我尝试使用useParams(),但是我发现很难实现...

I also tried to use matchPath, but I can find few examples.我也尝试过使用 matchPath,但我找不到几个例子。

Can anybody help me with this issue please?有人可以帮我解决这个问题吗?

Update:更新:

react-router-dom version: 6.8.0反应路由器-dom 版本:6.8.0

ResetPasswordConfirm is running in my container package. ResetPasswordConfirm 在我的容器 package 中运行。

import React, { useState } from "react";
import { Navigate, useParams } from 'react-router-dom';
import { connect } from 'react-redux';
import { reset_password_confirm } from '../actions/auth';

const ResetPasswordConfirm = ( {match, reset_password_confirm} ) => {

    const [requestSent, setRequestSent] = useState(false);
    const [formData, setFormData] = useState({
        new_password: '',
        re_new_password: ''
    });
    
const { new_password, re_new_password } = formData;

const onChange = e => setFormData({ ...formData, [e.target.name]: e.target.value });

const onSubmit = e=> {
    e.preventDefault();

    const uid = match.params.uid;
    const token = match.params.token;

    reset_password_confirm(uid, token, new_password, re_new_password);
    setRequestSent(true);
};

// Is the user sending the request?
// Redirect them to the home page
if (requestSent) {
    return <Navigate to='/'/>
}

return (
  <div className="container mt-5">
    <form onSubmit={e => onSubmit(e)}>
        {/* <div className="form-group">
            <input className="form-control" type="email" placeholder="Email" name="email" value={email} onChange={e => onChange(e)} required></input>
        </div>
        <div className="form-group">
            <input className="form-control" type="password" placeholder="Password" name="password" value={password} onChange={e => onChange(e)} minLength='6' required></input>
        </div>
        <button className="btn btn-primary" type="submit">Login</button> */}
        <div className="form-group">
            <label htmlFor="exampleInputPassword1">Password</label>
            <input 
            type="password" 
            className="form-control" 
            id="exampleInputPassword1" 
            placeholder="New Password"
            name="new_password"
            value={new_password}
            onChange={e => onChange(e)}
            minLength='6'
            required></input>
        </div>
        <div className="form-group">
            <label htmlFor="exampleInputPassword1">Password</label>
            <input 
            type="password" 
            className="form-control" 
            id="exampleInputPassword1" 
            placeholder="Confirm New Password"
            name="re_new_password"
            value={re_new_password}
            onChange={e => onChange(e)}
            minLength='6'
            required></input>
        </div>
        <button type="submit" className="btn btn-primary">Reset Password</button>
    </form>
  </div>  
);
};

export default connect(null, { reset_password_confirm }) (ResetPasswordConfirm);

Sorry for the lo-fi question.抱歉这个低保真问题。 I will try to do better.我会努力做得更好。

I tried to use useParams to fetch my uid我尝试使用 useParams 来获取我的 uid

const onSubmit = e => {
...
let{uid} = useParams()
}

There are no longer route props in react-router-dom routed components, ie there is not any injected match prop. react-router-dom路由组件中不再有路由道具,即没有任何注入的match道具。 You'll use the useParams hook to access the route path params in RRDv6.您将使用useParams挂钩访问 RRDv6 中的路由路径参数。 You can't use React hooks in nested callbacks, they must be called in the function body of a React component or in custom React hooks.您不能在嵌套回调中使用 React 挂钩,它们必须在 React 组件的 function 主体或自定义 React 挂钩中调用。 Use the useParams hook in the component body and destructure the uid and token path params.在组件主体中使用useParams挂钩并解构uidtoken路径参数。 Since you are using hooks you may as well also import and use the useNavigate hook and issue the navigation action at the end of the form's onSubmit handler instead of enqueueing a state update.由于您正在使用挂钩,因此您也可以导入和使用useNavigate挂钩,并在表单的onSubmit处理程序结束时发出导航操作,而不是将 state 更新加入队列。

import React, { useState } from "react";
import { useParams, useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { reset_password_confirm } from '../actions/auth';

const ResetPasswordConfirm = () => {
  const dispatch = useDispatch();
  const navigate = useNavigate();
  const { uid, token } = useParams();

  const [formData, setFormData] = useState({
    new_password: '',
    re_new_password: ''
  });
    
  const onChange = e => setFormData(formData => ({
    ...formData,
    [e.target.name]: e.target.value
  }));

  const onSubmit = e => {
    e.preventDefault();

    const { new_password, re_new_password } = formData;

    dispatch(reset_password_confirm(uid, token, new_password, re_new_password));
    navigate("/");
  };

  return (
    ...
  );
};

export default ResetPasswordConfirm;

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

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