Replies: 2 comments 2 replies
-
Instead of waiting for the search params to update, use the useDeferredValue hook to keep a state (used to render UI) and search params in sync without blocking the UI. The example in React docs is exactly about this case. function Component() {
let [state, setState] = useState(""); // The state
let deferredState = useDeferredValue(state); // Deferred to prevent blocking the render
let [, setSearchParams] = useSearchParams(); // Get the function to update the search params
useEffect(() => {
setSearchParams(param => {
params.set("key", deferredState) // update search params using the deferred state
return params;
});
}, [deferredState]);
// The UI only uses the state, not the deferred state or the search params
return <input value={state} onChange={event => setState(event.currentTarget.value)} />
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
Also, why hasn't a synchronous history replacement strategy made its way into the framework? Am I a weirdo with this edge case?useUpdateQueryStringValueWithoutNavigation |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've come to this section in the docs a few times in my adventures with Remix and I think there is a problem with the approaches to syncing inputs with search params.
My conclusion: there is no immediately reasonable way to update search params + state from clickable inputs (checkbox, radio)
The problem here is the delay with the loaders running:
There is a significant delay when the loaders are running. The delay is bad enough that the Hydrogen team wrote logic to use the navigation.location.search instead of relying on active searchParams.
The second choice is decent but doesn't actually change the URLSearchParams when the user changes the input 🤔
I think the only option left is to use logic to set the search params yourself, but then we're dependent on
setSearchParams()
which fires the loaders or a custom mechanism for simply replacing state (not pushing to history)Beta Was this translation helpful? Give feedback.
All reactions