0

I want this react component to show the navigation images based on a string array that's passed in as props. But my linter is blowing up

  Line 4:8:   'home' is defined but never used      no-unused-vars
  Line 5:8:   'menu' is defined but never used      no-unused-vars
  Line 6:8:   'bookings' is defined but never used  no-unused-vars
  Line 7:8:   'stock' is defined but never used     no-unused-vars
  Line 8:8:   'orders' is defined but never used    no-unused-vars
  Line 9:8:   'profile' is defined but never used   no-unused-vars
  Line 10:8:  'logout' is defined but never used    no-unused-vars

Is there some way to only import the images that are passed in on props and satisfy the linter?

import home from '../images/home.svg';
import menu from '../images/menu.svg';
import bookings from '../images/bookings.svg';
import stock from '../images/stock.svg';
import orders from '../images/orders.svg';
import profile from '../images/profile.svg';
import logout from '../images/logout.svg';

interface IProps {
  navItems: string[];
}

function Nav({ navItems }: IProps) {
  return (
    <div className='Nav'>
      {navItems.map((item, index) => {
        return <img src={item} id={String(index)} />;
      })}
    </div>
  );
}

export { Nav };

The navItems array looks like the below and maps to the image names exactly

  const navItems = [
    'home',
    'menu',
    'bookings',
    'stock',
    'orders',
    'profile',
    'logout',
  ];
  <Nav navItems={navItems} />

my eslint file...

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier"],
  "extends": ["eslint:recommended", "google", "prettier"],
  "rules": {
    "prettier/prettier": 2,
    "require-jsdoc": 0,
    "no-unused-vars": "off",
    "@typescript-eslint/no-unused-vars": ["error"]
  }
}

I've also tried this craziness but my typings are wrong

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

import home from '../images/home.svg';
import menu from '../images/menu.svg';
import bookings from '../images/bookings.svg';
import reports from '../images/stock.svg';
import refunds from '../images/orders.svg';
import profile from '../images/profile.svg';
import logout from '../images/logout.svg';


interface PropsShape {
  navItems: string[];
}

interface ImagesShape {
  home: ImageData;
  menu: ImageData;
  bookings: ImageData;
  reports: ImageData;
  refunds: ImageData;
  profile: ImageData;
  logout: ImageData;
}

function Nav({ navItems }: PropsShape) {
  const history = useHistory<History>();
  const navImages: ImagesShape = [
    { home },
    { menu },
    { bookings },
    { reports },
    { refunds },
    { profile },
    { logout },
  ];
  return (
    <div className='Nav'>
      {navItems.map((item, index) => {
        return (
          <img
            src={navImages[item]}
            id={String(index)}
            onClick={() => history.push(`/${item}`)}
            alt={item}
          />
        );
      })}
    </div>
  );
}

export { Nav };

Bill
  • 4,614
  • 13
  • 77
  • 132
  • Can you please share how does your `navItems` array look? – Nik Feb 11 '21 at 15:11
  • Added to the question, with thanks – Bill Feb 11 '21 at 15:14
  • 1
    You could try [dynamically requiring](https://stackoverflow.com/questions/52171142/importing-image-dynamically-react-js-require-img-path-cannot-find-module/52171663#52171663) them instead. Otherwise create an object of the image imports which implements a `Record` and access them by property name. – lawrence-witt Feb 11 '21 at 15:39
  • I tried `src={require(`../images/${item}.svg`)}` And I got `Error: Cannot find module './reports.svg` do the images need to be moved to the public folder? – Bill Feb 11 '21 at 15:57
  • Does this answer your question? [Import image dynamically in React component](https://stackoverflow.com/questions/53775936/import-image-dynamically-in-react-component) – Adam Feb 12 '21 at 01:58

0 Answers0