I am trying to pass props in order to use the React Router as part of a project I am in. I keep getting the error from this code that links is null, and I am not sure why –
Header component:
[![header][1]][1]
export default function Nav({ links }) {
return (
<nav className="navbar navbar-expand-lg bg-secondary">
<div className="container-fluid">
<div className="collapse navbar-collapse" id="navbarSupportedContent">
<ul className="navbar-nav me-auto mb-2 mb-lg-0">
{links.map((link, index) => (
<li key={index}>{link}</li>
))}
</ul>
</div>
</div>
</nav>
);
}
Navigation Component:
[![nav][2]][2]
// Bringing in the required import from 'react-router-dom'
import { Link } from 'react-router-dom';
import Header from './Header';
export default function Nav() {
// The Navbar UI component will render each of the Link elements in the links prop
return (
<Header
links={[
<Link key={1} className="nav-link text-light" to="/about">
Home
</Link>,
<Link key={2} className="nav-link text-light" to="/contact">
About Us
</Link>,
<Link key={3} className="nav-link text-light" to="/portfolio">
Portfolio
</Link>,
<Link key={4} className="nav-link text-light" to="/resume">
Portfolio
</Link>,
]}
/>
);
}
Error message I am receiving:
[![error][3]][3]
TypeError: Cannot read properties of undefined (reading 'map')
at Nav (http://127.0.0.1:3000/src/components/Header.jsx?t=1701476841986:18:379)
at renderWithHooks (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:12169:26)
at mountIndeterminateComponent (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:14919:21)
at beginWork (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:15900:22)
at beginWork$1 (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:19747:22)
at performUnitOfWork (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:19192:20)
at workLoopSync (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:19131:13)
at renderRootSync (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:19110:15)
at recoverFromConcurrentError (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:18730:28)
at performConcurrentWorkOnRoot (http://127.0.0.1:3000/node_modules/.vite/deps/chunk-MMW4JUSU.js?v=39b7b9ed:18678:30)
Any help would be greatly appreciated. Thank you!!
I have used ChatGPT extensively and cannot figure out this issue
[1]: https://i.stack.imgur.com/bwZRi.png
[2]: https://i.stack.imgur.com/YeJVh.png
[3]: https://i.stack.imgur.com/8TlNg.png
2
Answers
Personally I would not be passing the JSX as array values in a property the way you are. Recognize that JSX is really just a short-hand to calling functions (components) while providing context for the parents and children around those components. The way you have the
links
parameter, it is passing elements that are the result of nested function calls not in a place where React is expecting those functions (components) to be called.The way I’d approach this would be:
and then:
I couldn’t figure out cause of issue, rewrote from scratch, it worked.
Navbar.js
styles.css
If you wanna play around with code
Use preview link of codesanbox on your browser to view routing.