getQueryObject

Johny Kim·2021년 6월 15일
0

Custom Hooks

목록 보기
3/3

쿼리를 Object로 반환해주는 함수

const getQueryObj = <T extends {}> (search: string): T => {
  const arr = window.location.search.substr(1).split('&');
  return arr.reduce((prev, curr) => {
    const split = curr.split('=');
    return { ...prev, [split[0]]: split[1] }
  }, {} as T);
}

http://example.com/user?email=hello@world.com&name=helloworld

const queryObj = getQueryObj<{ email: string, name: string }>(window.location.search);
// { email: 'hello@world.com', name: 'helloworld' }
profile
작고 단단한 컴포넌트를 만들자.

0개의 댓글