javascript - 如何解决“未捕获的类型错误:无法读取未定义的属性'参数'”reactjs + django
问题描述
我正在练习 reactjs 看这个视频https://www.youtube.com/watch?v=5rh853GTgKo&list=PLJRGQoqpRwdfoa9591BcUS6NmMpZcvFsM&index=9
我想使用 uid 和 token 验证我的信息,但我不知道如何传递它。
在此代码中:Activate.js in container
import React, { useState } from 'react';
import { Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { verify } from '../actions/auth';
const Activate = ({ verify, match }) => {
const [verified, setVerified] = useState(false);
const verify_account = e => {
const uid = match.params.uid; // I Think This part is Problem
const token = match.params.token;
verify(uid, token);
setVerified(true);
};
if (verified) {
return <Redirect to='/' />
}
这段代码:auth.js in actions
export const verify = (uid, token) => async dispatch => {
const config = {
headers: {
'Content-Type': 'application/json'
}
};
const body = JSON.stringify({ uid, token });
try {
await axios.post(`${process.env.REACT_APP_API_URL}/auth/users/activation/`, body, config);
dispatch ({
type: ACTIVATION_SUCCESS,
});
} catch (err) {
dispatch ({
type: ACTIVATION_FAIL
});
}
}
我想我没有渲染uid,token,但我很困惑如何做到这一点
App.js 代码:
<Router>
<Layout>
<Switch>
<Route exact path ='/activate/:uid/:token'>
<Activate />
</Route>
</Switch>
</Layout>
</Router>
我会很感激任何帮助。:)
解决方案
使用useParams挂钩提取uid
和token
参数:
import React, { useState } from 'react';
import { Redirect, useParams } from 'react-router-dom';
import { connect } from 'react-redux';
import { verify } from '../actions/auth';
const Activate = ({ verify }) => {
const [verified, setVerified] = useState(false);
const { uid, token } = useParams();
const verify_account = e => {
verify(uid, token);
setVerified(true);
};
if (verified) {
return <Redirect to='/' />
}
推荐阅读
- python - 为什么使用 python 在我的 sqlite 表中搜索项目仅在数据是保存为文本的数字时才有效?
- json - Swift:如何处理相同值但返回类型不同的 JSON 响应?
- javascript - 二维数组从目标位置查找值
- keras - 用于非图像数据格式的多任务学习的 keras 数据生成器
- php - 用于 PHP 的 openssl_error_string() 在 Windows 开发环境中不起作用
- python - 如何调整 3dbar 图中 y 轴的比例以避免图中重叠
- flutter - 访问 Flutter 主题信息时无效的常量 value.dart(invalid_constant)
- python - Python GStreamer 寻求 https 音频中断播放
- java - 在 jbehave 故事文件中注释故事情节
- angular - 无法打开模态:角度