Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useQueryState : TypeError: Cannot read properties of null (reading 'toString') #780

Closed
Kamalito75 opened this issue Nov 22, 2024 · 8 comments · Fixed by #781
Closed

useQueryState : TypeError: Cannot read properties of null (reading 'toString') #780

Kamalito75 opened this issue Nov 22, 2024 · 8 comments · Fixed by #781
Labels
bug Something isn't working released

Comments

@Kamalito75
Copy link

Kamalito75 commented Nov 22, 2024

Context

What's your version of nuqs?

"nuqs": "^2.2.2"

What framework are you using?

  • ✅ Next.js (app router)

Which version of your framework are you using?

"next": "14.2.5"

Description

I have a hook (useCreateSessionModal) to display a modal depending on the value of "create-session" from useQueryState :

'use client'

import { parseAsBoolean, useQueryState } from "nuqs";

export const useCreateSessionModal = () => {
  const [isOpen, setIsOpen] = useQueryState(
    "create-session",
    parseAsBoolean.withDefault(false).withOptions({ clearOnDefault: true })
  );

  const open = () => setIsOpen(true);
  const close = () => setIsOpen(false);

  return {
    isOpen,
    open,
    close,
    setIsOpen,
  };
};

I have the following error :
TypeError: Cannot read properties of null (reading 'toString')

localhost-3000-11-22-2024_06_36_PM

Reproduction

@Kamalito75 Kamalito75 added the bug Something isn't working label Nov 22, 2024
@franky47
Copy link
Member

Do you have more info of where the error is thrown from?

FYI, you don't need to specify clearOnDefault: true (it's the default value)

@Kamalito75
Copy link
Author

I'm calling useCreateSessionModal from a client component (named client.tsx):

"use client";

import { useCreateSessionModal } from "@/features/sessions/hooks/use-create-session-modal";

const AccountIdClient = () => {
  const { open: createSession } = useCreateSessionModal();

This client component is used in a page.tsx file :

import React from 'react'
import AccountIdClient from './client'

export default function AccountPage() {
  return (
    <AccountIdClient />
  )
}

@Kamalito75
Copy link
Author

The error is caused by the absence of the "create-session" URL parameter on the page that invokes the hook.
Should I check for the presence of create-session in the URL before calling useQueryState?

@franky47
Copy link
Member

franky47 commented Nov 22, 2024

No, it takes care of that for you. When the search param is not present in the URL, the returned state is the default value.

Does the error pop up when rendering the component or is it when triggering an event?

Are you using the React Compiler by any chance?

If all else fails, could you please provide a minimal reproduction?

@Kamalito75
Copy link
Author

The error pops up when the component is rendered.
I'm building a minimal reproduction.

@franky47
Copy link
Member

franky47 commented Nov 22, 2024

I see one case where a null error would occur (see #781), though it's not on a .toString call, could you try this and let me know if it helps please?

pnpm add https://pkg.pr.new/nuqs@781

@Kamalito75
Copy link
Author

Thanks for your help François. It solved my problem. Have a nice weekend.

@franky47 franky47 added this to the 🚀 Shipping next milestone Nov 23, 2024
Copy link

🎉 This issue has been resolved in version 2.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@franky47 franky47 removed this from the 🚀 Shipping next milestone Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants