Search code examples
javascriptreactjsaxiosreact-routerhttprequest

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.


Solution

  • think u are looking for something like below:

    params: {
       ...(ID && {
          ID: ID
        }) // conditional spread operator
    }