I'm trying to retrieve product id from the url and pass it to the function. I've tried to use useParams but I couldn't figure out how to use it.
I think match.params.id working with react router v5 and they changed it in v6.
Here is my ProductDetails.js
import React, { useEffect} from 'react'
import { useParams } from 'react-router-dom';
import { useAlert } from 'react-alert';
import { useDispatch, useSelector } from 'react-redux'
import { getProductDetails, clearErrors } from '../../actions/productActions'
const ProductDetails = ({match}) => {
let params = useParams();
const dispatch = useDispatch();
const alert = useAlert();
const { loading, error, product } = useSelector(state => state.productDetails);
useEffect(() => {
dispatch(getProductDetails(match.params.id));
if(error){
alert.error(error);
dispatch(clearErrors());
}
}, [dispatch, alert, error, match.params.id]);
You should be able to retrieve the id
directly with:
const { id } = useParams();
And then use it with:
dispatch(getProductDetails(id));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments