Home > Net >  URLSearchParams with multiple parameters return space instead plus?
URLSearchParams with multiple parameters return space instead plus?

Time:10-26

I have a problem with decode URL. Instead my URL always return space ' '. I want to prevent it and return ( sign plus ) if exist.

Check my code:

function useSearchForDataQuery(parameters: URLSearchParams) { 
  const queryParamsData = new URLSearchParams(parameters);  
  // let testData = ['search', ...queryParams.values()];
  // console.log('testData', testData); 
  const { data, status, error, isRefetching } = useQuery<SearchResults>(
    ['search', ...queryParamsData.values()],
    () =>
      fetcher<SearchResults>(endpoint, {
        headers: {
          'Content-Type': 'application/json'
        },
        method: 'post',
        body: JSON.stringify(srcMapper(queryParamsData))
      })
  );

  return {
    searchResults: data, 
  };
}

This is custom hook but it not important.

Just check my console.log('test' ,test);

test is array with like a

[
  'search' ,
  'newParamsData query,
  'anotherParams'
]

Here is problem this -> 'newParamsData query,

Insted this parse space and I got like

[
  'search' ,
  'newParamsData query, ( here is problem because   is removed and here is just space
  'anotherParams'
]

To be clear.

parameters value which I got is newParamsData query

But URLSearchParams do something and I get the result

newParamsData query ( remove plus add space ) ( remove add ' ' )

What I try ?

  let decodedString = decodeURIComponent(parameter);
  const queryParamsData = new URLSearchParams(decodedString);

No work.

I am also try instead decodeURIComponent to add encodeURI, also no work?

How can my url not transform Plus into an empty string?

CodePudding user response:

Because plus is not use in params... params change the plus in space

CodePudding user response:

let assume the params is www.somewebsite.com?name=+somename

  const params = new URLSearchParams(props.location.search);

  console.log(params);

the console log look like this somename

  • Related