Coder Social home page Coder Social logo

Comments (2)

julia-script avatar julia-script commented on July 20, 2024

yep, it's related to that, even updating query params messes it up..I had to come up with a hack to work with search params

it only works with search params though, and I didn't implement the scroll flag cause I don't need it

"use client";
import { useSyncExternalStore } from "react";

const isServer = typeof window === "undefined";
class History {
  listeners = new Set<() => void>();

  constructor() {
    if (isServer) {
      return;
    }
    window.addEventListener("popstate", () => {
      this.emit();
    });
  }
  emit() {
    this.listeners.forEach((cb) => cb());
  }
  subscribe = (cb: () => void) => {
    this.listeners.add(cb);
    return () => {
      this.listeners.delete(cb);
    };
  };
  push = (url: string, _: { scroll: boolean }) => {
    if (isServer) {
      return;
    }
    window.history.pushState({}, "", url);
    this.emit();
  };
  replace = (url: string, _: { scroll: boolean }) => {
    if (isServer) {
      return;
    }
    window.history.replaceState({}, "", url);
    this.emit();
  };
  getSnapshot = () => {
    return new URL(window.location.href).searchParams.toString();
  };
}
const customHistory = new History();
export const _useRouter = () => {
  const searchParams = useSyncExternalStore(
    customHistory.subscribe,
    customHistory.getSnapshot,
    () => ""
  );

  return {
    searchParams,
    push: (url: string, options: { scroll: boolean }) => {
      customHistory.push(url, options);
    },
    replace: (url: string, options: { scroll: boolean }) => {
      customHistory.replace(url, options);
    },
  };
};

from awesome-fastapi-projects.

vladfedoriuk avatar vladfedoriuk commented on July 20, 2024

@julia-script Thanks for the input!
I believe a custom history could be a solution for that.
I see there is also some progress on vercel/next.js#48110, so maybe a fix will be delivered shortly in some future release.
I have recently found a package: https://github.com/47ng/next-usequerystate
What caught my eye is: https://github.com/47ng/next-usequerystate#shallow

Note: the app router doesn't vercel/next.js#48110 have this capability natively, but next-usequerystate does, by bypassing the router on shallow updates.

So, I think using this package until a proper fix is introduced in Next.js could be worth trying.

from awesome-fastapi-projects.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.