Search code examples
javascriptreactjsgetpath-variables

How to place path variable in Get request in React?


    componentDidMount() {
        axios.get('/api/v3/products', {
            params: {
                pageNumber: 1,
                pageSize: 500,
            }
        })

I know how to place params in Get request, but don't know how to place a path variable, could somebody help?

<Route path='/product/:id' component={Product}/>

tried to write "/product/:path", but I think, it doesn't make any sense.


Solution

  • Example Route: <Route path='/product/:productId' component={Product}/>

    Inside your component Product

    componentDidMount() {
        const { productId } = this.props.match.params
        // Extracted productId from the Route params.
        axios.get(`/api/v3/product/${productId}`, { // used productId in our GET Request
            params: {
                pageNumber: 1,
                pageSize: 500,
            }
        })
    }