Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
set multiple Search Params in the same render cycle

I use a Tree from mui v5 and I want to add in searchParams node that are selected and expanded. To do this I use the hook useSearchParams from React Router (v6).

The fact is that event selected and expanded are firing in the same rendering of the component.

So when the first write params by setSearchParams(...) the second do the same but with the same searchParams and erase params setted by the first.

I made a CodeSandBox which reproduces the behavior.

I try to use a ref to allow to mutate freshSearchParamsbut I did not succeed.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that the TreeView component is dispatching both onNodeSelect and onNodeToggle on the same Click. One thing you can do is customize both handleToggle and handleSelect functions, so they combine the two expanded and selected variables.

I'd take another approach to this scenario. I'd use a custom hook that handles the Tree state and wraps that state with that searchParams functionality. You can initialize the state from the URL and update the search parameters when the state is updated. I'd implement that URL update with a useEffect that compares status to URL and makes the appropriate updates.

Here's a possible implementation of that custom hook.

const useTreeUrlStatus = () => {
  const [searchParams, setSearchParams] = useSearchParams();
  const [state, setState] = useState(() => {
    return {
      selected: searchParams.get("selected") ?? "",
      expanded: searchParams.get("expanded")
        ? searchParams.get("expanded").split(",")
        : []
    };
  });

  useEffect(() => {
    if (
      searchParams.get("selected") !== state.selected ||
      searchParams.get("expanded") !== state.expanded
    ) {
      setSearchParams({
        selected: state.selected,
        expanded: state.expanded.join(",")
      });
    }
  }, [state, searchParams, setSearchParams]);

  const updateState = (key, value) => {
    setState((prevState) => {
      const newState = { ...prevState, [key]: value };
      return newState;
    });
  };

  return [state, updateState];
};

You have a working sandbox here forked from your posted sandbox.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!