简体   繁体   中英

× TypeError: Cannot read properties of undefined (reading 'push')

enter image description here

I was able to solve this issue using useNavigate

let params = useParams();
  let navigate = useNavigate();
  const dispatch = useDispatch();
  const productId = params.id;
  const [qty, setQty] = useState(1);
  const productDetails = useSelector((state) => state.productDetails);
  const { loading, error, product } = productDetails;
 
  useEffect(() => {
    dispatch(detailsProduct(productId));
  }, [dispatch, productId]);
 
  const addToCartHandler = () => {
    navigate(`/cart/${productId}?qty=${qty}`);
  };
  

but the page didnt show anything enter image description here

enter image description here

thenk you

While you are using react functional componenets.you should use hooks

import {useHistory} from 'react-router-dom'
const App=()=>{
  const history=useHistory()
  const submit=()=>{
     history.push("/products/23")
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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