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

subscribeToQueryUpdates is not reactive when using Next.js v14.0.3 #422

Closed
lamualfa opened this issue Dec 4, 2023 · 6 comments · Fixed by #425
Closed

subscribeToQueryUpdates is not reactive when using Next.js v14.0.3 #422

lamualfa opened this issue Dec 4, 2023 · 6 comments · Fixed by #425
Labels

Comments

@lamualfa
Copy link

lamualfa commented Dec 4, 2023

Reproduction

https://codesandbox.io/p/devbox/winter-sound-6y9rv3

Preview Code

"use client";

import { useQueryState, subscribeToQueryUpdates } from "next-usequerystate";
import { useEffect, useState } from "react";

function Search() {
  const [search, setSearch] = useQueryState("search");

  return (
    <input
      placeholder="Search"
      value={search ?? ""}
      onChange={(e) => {
        setSearch(e.target.value);
      }}
    />
  );
}

function ResultManualSubcription() {
  const [params, setParams] = useState<URLSearchParams | undefined>();

  useEffect(() => {
    subscribeToQueryUpdates(({ search }) => {
      setParams(search);
    });
  }, []);

  return (
    <p>
      <b>Manual subscription</b>: {params?.get("search")}
    </p>
  );
}

function ResultNextQueryState() {
  const [search] = useQueryState("search");

  return (
    <p>
      <b>Next Query State:</b> {search}
    </p>
  );
}

export default function Page() {
  return (
    <main>
      <section>
        <Search />
      </section>
      <section>
        <h2>Result</h2>

        <ResultManualSubcription />
        <ResultNextQueryState />
      </section>
    </main>
  );
}

Tech Stack

  • react 18
  • next 14.0.3
  • next-usequerystate 1.13.0
  • ✅ The app router
  • ❌ The basePath option in your Next.js config
  • ❌ The experimental windowHistorySupport flag in your Next.js config
@lamualfa lamualfa added the bug Something isn't working label Dec 4, 2023
@franky47
Copy link
Member

franky47 commented Dec 4, 2023

Yes, I'm afraid that with recent changes in the app router core, I'll have to deprecate this function. If you turn on the windowHistorySupport flag, the useSearchParams hook becomes reactive to changes in the query string and can be used as a replacement.

@Talent30
Copy link
Contributor

Talent30 commented Dec 4, 2023

Yes, I'm afraid that with recent changes in the app router core, I'll have to deprecate this function. If you turn on the windowHistorySupport flag, the useSearchParams hook becomes reactive to changes in the query string and can be used as a replacement.

Road to v2?

@franky47
Copy link
Member

franky47 commented Dec 4, 2023

@Talent30 when the windowHistorySupport flag lands in stable as the default behaviour (which I hope it does), then yes we can remove this function, I'll just flag it as deprecated for now with a link to this issue.

But that's probably quite far down the line, I expect the Next.js team wants to get some folks onboard testing shallow routing first.

franky47 added a commit that referenced this issue Dec 8, 2023
It was broken in Next.js 14.0.3 due to shallow updates
not calling the patched history methods, and fixed in
Next.js 14.0.4.

However, since turning on the `windowHistorySupport` flag
causes `useSearchParams` to be reactive to shallow URL query
changes, there's no longer need for this method.

Marking it as deprecated now, and will be dropped along with
other deprecations in v2.0.0 once WHS lands in GA.

Closes #422.
@franky47
Copy link
Member

franky47 commented Dec 8, 2023

This is fixed in Next.js 14.0.4, both with or without the windowHistorySupport flag.

I'll still mark the method as deprecated as it's an internal thing that won't make much sense once proper reactivity of useSearchParams is reached in stock Next.js. See #425.

@franky47 franky47 closed this as completed Dec 8, 2023
franky47 added a commit that referenced this issue Dec 8, 2023
It was broken in Next.js 14.0.3 due to shallow updates
not calling the patched history methods, and fixed in
Next.js 14.0.4.

However, since turning on the `windowHistorySupport` flag
causes `useSearchParams` to be reactive to shallow URL query
changes, there's no longer need for this method.

Marking it as deprecated now, and will be dropped along with
other deprecations in v2.0.0 once WHS lands in GA.

Closes #422.
Copy link

github-actions bot commented Dec 8, 2023

🎉 This issue has been resolved in version 1.13.1-beta.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

github-actions bot commented Dec 8, 2023

🎉 This issue has been resolved in version 1.13.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants