Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making request parameter optional in axios get request

I have below axios request.

import axios from 'axios';
axios.get('/user', {
    params: {
      ID
      Name
    }
  })
  .then(function (response) {
    console.log(response);
  })

I want all the request parameter should be optional. if user send ID then it should give ID, if user enter ID and Name it should consider both. If user enters none it should display all the record.

I dont know how to handle this in react.

like image 987
Shruti sharma Avatar asked Dec 30 '25 17:12

Shruti sharma


1 Answers

think u are looking for something like below:

params: {
   ...(ID && {
      ID: ID
    }) // conditional spread operator
}
like image 70
arbitrary_A Avatar answered Jan 02 '26 09:01

arbitrary_A



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!