ube*_*ebu 6 reactjs react-router react-router-dom
我正在尝试使用 React 设置验证帐户,并在我的App.js文件中包含以下内容
应用程序.js
import SigninPage from './pages/signin';
import ResetPasswordPage from './pages/resetPassword'
import VerifyAccountPage from './pages/verifyAccount'
...
...
import { useHistory } from 'react-router';
import { logout } from './utils/auth';
function App() {
const history = useHistory();
return (
<Router>
<Switch>
<Route path='/' component={Home} exact />
<Route
path="/signout"
render={() => {
logout();
history.push('/');
return null;
}}
/>
<Route path='/signin' component={SigninPage} exact />
<Route path='/reset-password?reset-password-token=:resetPasswordToken' component={ResetPasswordPage} />
<Route path='/verify-account?token=:token&email=:email' component={VerifyAccountPage} exact />
</Switch>
</Router>
);
}
export default App;
Run Code Online (Sandbox Code Playgroud)
在我的VerifyAccountPage组件中有以下内容
import { Redirect } from 'react-router-dom';
import { useHistory } from 'react-router';
import { verifyAccount, isAuthenticated } from '../../utils/auth';
const VerifyAccount = () => {
const { token, email } = this.props.match.params
const history = useHistory();
const [error, setError] = useState('');
const handleGet = async (e) => {
e.preventDefault();
setError('');
try {
const data = await verifyAccount(token, email);
if (data) {
history.push('/');
}
console.log(data);
} catch (err) {
if (err instanceof Error) {
// handle errors thrown from frontend
setError(err.message);
} else {
// handle errors thrown from backend
setError(err);
}
}
};
return isAuthenticated() ? (
<Redirect to="/#" />
) : (
<>
<Container>
<FormWrap>
<Icon to='/'>mywebsite</Icon>
<FormContent>
<Form action='#'>
<FormH1>Verify Account</FormH1>
<Text>Account has been verified!</Text>
</Form>
</FormContent>
</FormWrap>
</Container>
</>
);
};
export default VerifyAccount;
Run Code Online (Sandbox Code Playgroud)
这是verifyAccount.js页面
import React from 'react';
import VerifyAccount from '../components/VerifyAccount';
import ScrollToTop from '../components/ScrollToTop';
function VerifyAccountPage() {
return (
<>
<ScrollToTop />
<VerifyAccount />
</>
);
}
export default VerifyAccountPage;
Run Code Online (Sandbox Code Playgroud)
这是
索引.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
Run Code Online (Sandbox Code Playgroud)
但这不起作用,当我转到该链接时,https://mywebsite.com/verify-account?token=3heubek&email=user1@email.com除了 200 或 304 状态代码之外没有任何反应
没有请求发送到 API,因此意味着参数没有被提取
谁能告诉我发生了什么事吗?
package.json 文件中使用的包版本
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-icons": "^3.11.0",
"react-router-dom": "^5.2.0",
"react-scripts": "3.4.3",
"react-scroll": "^1.8.1",
"styled-components": "^5.2.0",
"@types/node": "^15.6.0",
"jwt-decode": "^3.0.0"
Run Code Online (Sandbox Code Playgroud)
Dre*_*ese 14
路由匹配参数与 URL 查询字符串参数不同。
您需要从位置对象访问查询字符串。
Run Code Online (Sandbox Code Playgroud){ key: 'ac3df4', // not with HashHistory! pathname: '/somewhere', search: '?some=search-string', <-- query string hash: '#howdy', state: { [userDefined]: true } }
他们创建了一个自定义useQuery钩子:
const useQuery = () => new URLSearchParams(useLocation().search);
Run Code Online (Sandbox Code Playgroud)
对于您的用例,在呈现您想要的页面上VerifyAccountPage然后提取查询字符串参数。鉴于path='/verify-account?token=:token&email=:email':
const query = useQuery();
const email = query.get('email');
const token = query.get('token');
Run Code Online (Sandbox Code Playgroud)
如果VerifyAccountPage是基于类的组件,那么您将需要props.location在生命周期方法中自己访问和处理查询字符串。这是因为 React hooks 仅由功能组件有效使用。
componentDidMount() {
const { location } = this.props;
const query = new URLSearchParams(location.search);
const email = query.get('email');
const token = query.get('token');
...
}
Run Code Online (Sandbox Code Playgroud)
path='/verify-account?token=:token&email=:email'
Run Code Online (Sandbox Code Playgroud)
路径参数仅与 URL 的路径部分相关,您无法为 URL 的 queryString 部分定义参数。上面的路径相当于path='/verify-account'fromreact-router-dom的视角。
您可以用于withRouter获取路由器信息,例如位置、历史记录、路径和参数。
import { withRouter } from "react-router-dom";
...
const VerifyAccount = withRouter(props) => {
const { token, email } = props.match.params;
console.log("toke, email: ", token, email) // And also show me how it looks!!!
...
}
Run Code Online (Sandbox Code Playgroud)
你应该像这样定义你的路线。
<Route path='/verify-account/:token/:email' component={VerifyAccountPage} />
Run Code Online (Sandbox Code Playgroud)
你应该这样调用:
https://your-domain/verify-account/[token]/[email]
Run Code Online (Sandbox Code Playgroud)
这会起作用。
| 归档时间: |
|
| 查看次数: |
50096 次 |
| 最近记录: |