Error: Mapping an object with an integrated react-icon

Viewed 39

I've got an object that I'm trying to map over. I keep getting this error:

SyntaxError: Unexpected token '<' (at navigation.js?t=1663213543000:18:11).

It's referring to the icon. It should work though... React-Icons "^4.4.0" is installed.

Here's a sandbox recreation without the issue...

https://codesandbox.io/s/brave-sea-sholnc?file=/src/navigation.js

enter image description here



navigation.js

import React from 'react';
import { RiContactsLine } from 'react-icons/ri';

export const links = [
   {
      title: 'Dashboard',
      links: [
           {
              name: 'ecommerce',
              icon: <RiContactsLine />,
           },
      ],
   }
]

If I comment out the icon line, it'll display Dashboard and ecommerce, but won't render with the icon line included. Here's the file that maps over it:

Sidebar.jsx

import { links } from '../../Data/navigation.js';
import { Link, NavLink } from "react-router-dom";

const Sidebar = () => {

  return (

    <div className="sidebar-container">
        <div className="sidebar shadow-md">

            {links.map((item) => (
              <div key={item.title}>
                <p className="text-gray-400 m-3 mt-4 uppercase title">
                  {item.title}
                </p>
                {item.links.map((link) => (
                  <NavLink 
                    to={`/${link.name}`}
                    key={link.name}
                  >
                      {link.icon}
                      <span className="capitalize">
                          {link.name}
                      </span>
                  </NavLink>
                ))}
               </div>
              ))}
        </div>
    </div>
  )
}

export default Sidebar

Please help! Thanks!

1 Answers

Solved. After deleting node_modules and reinstalling, I received this error.

"✘ [ERROR] The JSX syntax extension is not currently enabled src/Data/navigation.js:22:22: 22 │ icon: ,"

After following this link: https://badcodernocookie.com/support-for-the-experimental-syntax-jsx-isnt-currently-enabled/

It looks like it had something to do with Vite...

I changed the filename to navigation.jsx, created the .babelrc file in the main directory, and installed those 2 dependencies. Hope this can help someone!

Related