How to get id from the url react for dispatch instead of using params?

ademclk

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]);
lpizzinidev

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to get query params from url in Angular 2?

How to get track ID from URL using the SoundCloud API

Get route params from url

Angular 2: how to get params from URL without using Routing?

How to get id/params to redux action from react Router?

How to get params url parent id from children

How to get id from route params and pass into a service

Can't get id from URL using React Router DOM

How to get Params from url for Axios GET request?

how get react router origin url without params?

How to get id from the URL using snapshot or ActivatedRoute subscriber in Angular?

How to get the id parameter of a url when the props.match.params.id returns undefined but the id is visible on the url?

get id from url in react.js

How to get Params from the URL to the Authentification function? (Python, Flask)

How to get params id in react class component

How to include a param in browsers url when using params instead of url in angular ui routing?

how to get id from URL using php

Rails: How to pass params from a friendly_id url to a controller?

How to get url params in cakephp?

how to get last id number from url using php

how to compare the query value from url to id using react?

Getting params from url React

How to get URL params when using Custom Document in NextJS

How to pass location state and URL params using React-Router?

How to get params from URI scheme of deep link in React navigation?

How to send url params from react to express api call

react get id from url with component

Unable to get id from the URL using params

How to restrict url params to a certain set of values using react router?