Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting undefined in react routing properties

I want to send some properties (loading and getContacts) to a Component in reactjs . I use routes in order to get each path, but the result in the target component is undefined. Whats wrong with it?

const App = () => {
  
  const [getContacts , setContacts] = useState([]);
  const [loading , setLoading] = useState(false);
  
  return(
    <div className='App'>
        <Navbar/>
       
        <Routes>
          <Route path='/' element = {<Navigate to ="/contacts"/>}/>

          <Route path='/contacts' loading= {loading} contacts= {getContacts} element= {<Contacts/>} />

          <Route path="/contacts/add" element = {<AddContact/>}/>

          <Route path = "/contacts/:contactId" element = {<ViewContact/>}/>

          <Route path = "/contacts/edit/:contactId" element = {<EditContact/>}/>

          <Route path="*" element={

              <div className='text-light py-5'>
                Not Found!
              </div>}

          />

        </Routes>

    </div>
);
export default App;

In Contact.jsx I have:

const Contacts = ({ contacts, loading }) => {

    return (
        <>
            <div>{contacts}</div>
            <div>{loading}</div>
        </>
    );
};

export default Contacts;

But both of them are undefined.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try placing the state variables directly into the children element :

<Route path='/contacts' element={<Contacts loading={loading} contacts={getContacts}/>} />
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading