I was setting up a react pro sidebar:
import { HiInbox } from "react-icons/hi";
<MenuItem component={<Link to="/documentation" />} icon={<HiInbox />}> Documentation</MenuItem>
Does anyone know what the syntax of icon={<HiInbox />}
means? How is it different than icon={HiInbox}
which doesn’t work?
And why does flowbite react sidebar syntax not need it?
<Sidebar.Item href="#" icon={HiChartPie}>
2
Answers
It means that the
icon
property ofMenuItem
accepts a jsx fragment.When you use a component (functional or class-based) in JSX:
It is similar to running the function
Component()
, but in a syntax that is easier to use for hierarchal markup.Therefore it depends on the implementation of the component as different components will expect to take in a jsx component directly (Like with
MenuItem
), and others will take in the component and then build a jsx component from it themselves (As withSidebar.Item
).Here’s an analogous example with functions instead of components:
In this example,
takes_function
takes in a function that outputs a number (Analagous toSidebar.Item
) whiletakes_result
takes in the result of a function (Analagous toMenuItem
).Basically, it just depends on the internal design of the component you are importing. So you just need to give each prop what it asks for.