1

A JSON array of icons with names is available. I want to map JSX but nothing shows as JSX element (icons) in the output, the code uses Material-UI elements and Icons:

import * as React from 'react';
import {Box, Paper, Typography} from '@mui/material';
import WorkIcon from '@mui/icons-material/Work';
import Settings from '@mui/icons-material/Settings';
const iconsSet = [
  {
    name: 'WorkIcon',
    icon: ()=> (<WorkIcon/>),   {/* JSX as fields value */}
  },
  {
    name: 'Settings',
    icon: () =>(<Settings/>),
  }
];
export default function Icons() {
  return (
    <Box>
      {iconsSet.map(({name,icon})=>(
        <Paper>
          {icon} {/* Nothing Shows  */}
          <Typography>{name}</Typography>
        </Paper>
      ))}
    </Box>
    );
}

link to Codesandbox

2
  • Are there any errors in the console or any other information to help debug this? Commented Apr 14, 2022 at 18:17
  • Everything looks good, but nothing shows Commented Apr 14, 2022 at 18:21

2 Answers 2

2

You have to name JSX elements with capital first letter, try Icon in the object key:

const iconsSet = [
  {
    name: 'WorkIcon',
    Icon: ()=> (<WorkIcon/>),   {/* JSX as fields value */}
  },
  {
    name: 'Settings',
    Icon: () =>(<Settings/>),
  }
];

And then use it in JSX inside the map() like this:

<Box>
  {iconsSet.map(({name,Icon})=>(
    <Paper>
      <Icon />
      <Typography>{name}</Typography>
    </Paper>
  ))}
</Box>
Sign up to request clarification or add additional context in comments.

Comments

0

try

import * as React from 'react';
import {Box, Paper, Typography} from '@mui/material';
import WorkIcon from '@mui/icons-material/Work';
import Settings from '@mui/icons-material/Settings';
const iconsSet = [
  {
    name: 'WorkIcon',
    icon: ()=> (<WorkIcon/>),   {/* JSX as fields value */}
  },
  {
    name: 'Settings',
    icon: () =>(<Settings/>),
  }
];
export default function Icons() {
  return (
    <Box>
      {iconsSet.map(({name,icon})=>(
        <Paper>
          {icon()} {/* here is the change  */}
          <Typography>{name}</Typography>
        </Paper>
      ))}
    </Box>
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.