DEV Community

Richard
Richard

Posted on • Edited on

1 1

react-router-dom question: After I setSearchParamaters, why does searchParamaters.get fail to retreive current value?

import { useSearchParams } from "react-router-dom";

//in the component:
const [searchParams, setSearchParams] = useSearchParams()

//in the render section:
<button
        onClick={() => {
          setSearchParams({
            filter: "alligators",
          });
          console.log(searchParams.get("filter"));
        }}
      >
        setSearchParams and view the value
      </button>
Enter fullscreen mode Exit fullscreen mode

After I click the button, I do not see 'alligators' logged! Instead, what gets logged is the previous filter value. In order to actually see 'alligators', I have to press the button twice. Why doesn't it work the first time I click it? Thanks!

Top comments (0)

nextjs tutorial video

Youtube Tutorial Series 📺

So you built a Next.js app, but you need a clear view of the entire operation flow to be able to identify performance bottlenecks before you launch. But how do you get started? Get the essentials on tracing for Next.js from @nikolovlazar in this video series 👀

Watch the Youtube series

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay