[英]TypeError: Cannot read properties of undefined (reading 'search')
I am completely new to this.我对此完全陌生。 Tried for hours to find some sort of explanation on this.尝试了几个小时来找到对此的某种解释。 Basically I am trying to replicate a project to understand the main concepts.基本上我正在尝试复制一个项目以了解主要概念。 In the project the code apparently works just fine, I however receive a "TypeError: Cannot read properties of undefined (reading 'search')" error.在项目中,代码显然工作得很好,但是我收到“TypeError:无法读取未定义的属性(读取'搜索')”错误。 I hope that someone here can explain to me, what I am missing.我希望这里有人可以向我解释我所缺少的。
I believe the Errors to come from the following lines of code:我相信错误来自以下代码行:
SigninScreen:登录屏幕:
const redirect = props.location.search
? props.location.search.split("=")[1]
: "/";
ProductScreen:产品屏幕:
const productId = props.match.params.id;
Kind Regards,亲切的问候,
Makani马卡尼
SigninScreen:登录屏幕:
import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Link, } from "react-router-dom";
import { signin } from "../actions/userActions";
import Loadingbox from "../components/LoadingBox";
import MessageBox from "../components/MessageBox";
export default function SigninScreen(props) {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const redirect = props.location.search
? props.location.search.split("=")[1]
: "/";
const userSignin = useSelector((state) => state.userSignin);
const { userInfo, loading, error } = userSignin;
const dispatch = useDispatch();
const submitHandler = (e) => {
e.preventDefault();
dispatch(signin(email, password));
};
useEffect(() => {
if (userInfo) {
props.history.push(redirect);
}
}, [props.history, redirect, userInfo]);
return ( ... );
}
ProductScreen:产品屏幕:
import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Link, useParams } from "react-router-dom";
import { detailsProduct } from "../actions/productActions";
import LoadingBox from "../components/LoadingBox";
import MessageBox from "../components/MessageBox";
import Rating from "../components/Rating";
export default function ProductScreen(props) {
const productId = props.match.params.id;
const dispatch = useDispatch();
const [qty, setQty] = useState(1);
const productDetails = useSelector((state) => state.productDetails);
const { loading, error, product } = productDetails;
useEffect(() => {
dispatch(detailsProduct(productId));
}, [dispatch, productId]);
const addToCartHandler = () => {
props.history.push(`/cart/${productId}?qty=${qty}`);
};
return ( ... );
}
From the documentation for React Router v5 :从React Router v5 的文档中:
The router will provide you with a location object in a few places:
· Route component as this.props.location
· Route render as ({ location }) => ()
· Route children as ({ location }) => ()
· withRouter as this.props.location
A component receives the location
prop automatically in 4 usages mentioned about.组件在提到的 4 种用法中自动接收location
属性。 Since you have not described withRouter
, I can only assume that your case is Option 1.由于您没有描述withRouter
,我只能假设您的情况是选项 1。
React Router adds the location
prop to the component automatically if it is directly in the Route component.如果直接在 Route 组件中,React Router 会自动将location
属性添加到组件中。
// if this is the case, SignIn receives a location prop
<Route path="/path" component={SigninScreen} />
If you want to add location
prop manually, you can use withRouter
higher order component.如果要手动添加location
属性,可以使用withRouter
高阶组件。
export default withRouter(SignInScreen);
I had same error.我有同样的错误。 In v6 you have to use useLocaion()
like:在 v6 中,您必须使用useLocaion()
,例如:
const location = useLocation();
const redirect = location.search
? location.search.split('=')[1]
: '/';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.