我试图导航到我的发货页面,但我一直收到此错误
Im trying to navigate to my shipping page but i keep getting this error
这是错误信息
react-dom.development.js:11340 Uncaught TypeError: Cannot read properties of undefined (reading 'search')
at Login (Login.js:21:1)
at renderWithHooks (react-dom.development.js:14985:1)
at mountIndeterminateComponent (react-dom.development.js:17811:1)
at beginWork (react-dom.development.js:19049:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
at invokeGuardedCallback (react-dom.development.js:4056:1)
at beginWork (react-dom.development.js:23964:1)
at performUnitOfWork (react-dom.development.js:22776:1)
at workLoopSync (react-dom.development.js:22707:1)
这是我的代码
我的Login.js
const Login = ({ history, location }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const alert = useAlert();
const dispatch = useDispatch();
const { isAuthenticated, error, loading } = useSelector(state => state.auth);
const redirect = location.search ? location.search.split('=')[1] : '/'
const navigate = useNavigate();
// navigate('/')
useEffect(() => {
if (isAuthenticated) {
navigate(redirect)
}
if (error) {
alert.error(error);
dispatch(clearErrors());
}
}, [dispatch, alert, isAuthenticated, error, navigate, redirect])
const submitHandler = (e) => {
e.preventDefault();
dispatch(login(email, password))
}
return ()} export default Login
错误似乎来自我的“location.search”,因为在我的 vscode 中,那部分是第 21 行。最近文档有任何更改吗?
location
& history
道具从何而来?
如果你正在使用钩子,我认为这不应该来自道具,而是来自钩子
import { useHistory, useLocation } from "react-router-dom";
const Login = () => {
const history = useHistory();
const location = useLocation();
...
}
这是错误信息
react-dom.development.js:11340 Uncaught TypeError: Cannot read properties of undefined (reading 'search')
at Login (Login.js:21:1)
at renderWithHooks (react-dom.development.js:14985:1)
at mountIndeterminateComponent (react-dom.development.js:17811:1)
at beginWork (react-dom.development.js:19049:1)
at HTMLUnknownElement.callCallback (react-dom.development.js:3945:1)
at Object.invokeGuardedCallbackDev (react-dom.development.js:3994:1)
at invokeGuardedCallback (react-dom.development.js:4056:1)
at beginWork (react-dom.development.js:23964:1)
at performUnitOfWork (react-dom.development.js:22776:1)
at workLoopSync (react-dom.development.js:22707:1)
这是我的代码
我的Login.js
const Login = ({ history, location }) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const alert = useAlert();
const dispatch = useDispatch();
const { isAuthenticated, error, loading } = useSelector(state => state.auth);
const redirect = location.search ? location.search.split('=')[1] : '/'
const navigate = useNavigate();
// navigate('/')
useEffect(() => {
if (isAuthenticated) {
navigate(redirect)
}
if (error) {
alert.error(error);
dispatch(clearErrors());
}
}, [dispatch, alert, isAuthenticated, error, navigate, redirect])
const submitHandler = (e) => {
e.preventDefault();
dispatch(login(email, password))
}
return ()} export default Login
错误似乎来自我的“location.search”,因为在我的 vscode 中,那部分是第 21 行。最近文档有任何更改吗?
location
& history
道具从何而来?
如果你正在使用钩子,我认为这不应该来自道具,而是来自钩子
import { useHistory, useLocation } from "react-router-dom";
const Login = () => {
const history = useHistory();
const location = useLocation();
...
}