How to replace querystring.stringify with URLSearchParams?

Issue

        querystring.stringify({
          error: 'error_status'
        })

‘querystring’ is deprecated, how would I replace it with the native URLSearchParams here?

Solution

const params = new URLSearchParams({
  error: 'error_status'
});

console.log(params.toString());
// error=error_status

console.log(`?${params.toString()}`);
// ?error=error_status

More information at https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

Answered By – Ross

Answer Checked By – Clifford M. (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.