Passing set statehook function into subroute component. Destructuring error

twSoulz

Using react 18.2 with react-router-dom 6.3 I have my routes in this style

<Routes>
        <Route path="/" element = {<App/>}>
             <Route path="search" element = {<Content/>} />
             <Route path="nextComp/:id" element = {<Component/>} />
        </Route>
</Routes>

In my app, I have a dynamic nav bar that I want to stick around which could be used to select different generated components (generated by actions in the Content component in /search) There are states in my App component that need to be set by the Content component. I pass information down with something along the lines of:

const App: React.FC = () => {
    const [lock, setLock] = useState<Boolean>(false);
    const [lotsQuery, setLotsQuery] = useState<lotWaferQueryInput[]>([]);

    const navigate = useNavigate();
    const onClickHandle(() => {
        navigate('/search', {state: { 
            setLock : setLock,
            setLotsQuery: setLotsQuery
        }});
    }, []);
}

In my Content component, I try accessing the data with :

const {state} : any = useLocation();
const {setLock,setLotsQuery} : any = state;

This results in Uncaught TypeError: Cannot destructure property 'setLock' of 'state' as it is null. I understand that you can't directly serialize a function. How should I reapproach the way I'm routing data?

As of now, the hierarchy looks like

App
-Nav (child component)
-Content(search subroute)
-Component(nextComp subroute)

Data is entered in Content, and then sent to App (which is the current problem of being able to set function). Data is handled by App and then passed to nav and generates Component(subroutes)

How can I achieve sending data from a subroute component to a parent route then? Any advice appreciated.

Drew Reese

The route state needs to be JSON serializable, so sending functions just won't work. I suggest exposing the functions down to nested routes via the Outlet's context and the useOutletContext hook.

Example:

import { Outlet } from 'react-router-dom';

const App: React.FC = () => {
  const [lock, setLock] = useState<Boolean>(false);
  const [lotsQuery, setLotsQuery] = useState<lotWaferQueryInput[]>([]);

  ...

  return (
    ...
      <Outlet context={{ setLock, setLotsQuery }} />
    ...
  );
};

In nested route's component:

import { useOutletContext } from 'react-router-dom';

...

const { setLock, setLotsQuery } = useOutletContext();

...

<Routes>
  <Route path="/" element={<App />}> // <-- provides context value
    <Route path="search" element={<Content />} /> // <-- can access context value
    <Route path="nextComp/:id" element={<Component />} />
  </Route>
</Routes>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

component props destructuring error

React props destructuring when passing to component

TSLint error for destructuring in function parameters

Can I set default value to the function when passing it to a Child component?

Error when passing setState in a React class component to an external function?

Passing a Set to a Child Component in Angular

Passing Sklearn model to a function to set arguments - type error: not callable?

Error when trying to use Destructuring as function parameters

Passing set to templatised function

Angular 1.5 Component: passing a function

Passing props to a Function as a Child component

Passing a function to React component in props

(ReactJS) Function not passing to child component

Passing a function as a prop to a functional component

Passing an empty function into a React component

React passing a component as a param to a function

Issue passing function to child component

Blazor - Passing a function to dynamic component

Passing props to a component - syntax error

Passing main component state to another component error

Passing an array to function error?

How to render stateHook result from the function call in ReactJS

React Native - Bottom Tab Navigator - error - Passing an inline function will cause the component state to be lost on re-render?

react native : How to fix the error - Passing an inline function will cause the component state to be lost on re-render?

Typescript React/ styled-components - passing function as prop to component throws error on missing type

React functional component destructuring

Destructuring this.props into Component

Passing an optional function to a component best practice

Component crashes when passing in a function as a slot variable