React: avoid hooks that set default values, use getters with defaults

Published: (March 13, 2026 at 06:49 AM EDT)
1 min read
Source: Dev.to

Source: Dev.to

Problem

A colleague helped identify a pattern that can be a foot‑gun.
When a user opens mysite.com/?urlParam=foo, the app should load the correct FooComponent. If the site is opened without any query parameter, it should default to FooComponent as well.

Component Example

export const MyAdvancedComponent = () => {
  useSetDefaultParameters();

  const { params } = usePlaygroundContext();

  return (
    <>
      {params.foo === "foo" && <FooComponent />}
      {params.foo === "bar" && <BarComponent />}
      {/* …other components */}
    </>
  );
};

Hook Implementation

export const useSetDefaultParameters = () => {
  const urlParam = useGetUrlParam();

  const { setParam } = useMyContext();

  useEffect(() => {
    const defaultValue = defaults[urlParam];
    setParam(urlParam, defaultValue);
  }, [urlParam]);
};
0 views
Back to Blog

Related posts

Read more »

I wrote an amazing React Book

!Cover image for I wrote an amazing React Bookhttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-...