2

I'm using Next.js + typescript and doing the following:

const MyPage = (): JSX.Element => {
    const [status, setStatus] = useState("")
    const router = useRouter()
    useEffect(() => {
        if (router.isReady) {
            if (router.query.status) {
               setStatus(router.query.status)
            }
        }

    }, [router])
    return <div>Status: {status} </div>
}

What is the best way to check if router.query.status is string or []string ?

Should I do this?

if (Array.isArray(router.query.status)) {
    const y = router.query.status[0]
    setStatus(y)
}

I want to make sure that someone doesn't put extra parameters and break the code like this:

example.com/?status=foo&status=bar

which will result in router.query.status to be an array.

is there a better way to do that? or should I just keep using isArray method?

Arximede
  • 371
  • 1
  • 4
  • 17

0 Answers0