Skip to content

Custom JSX content for options labels and selected values #4300

Answered by ebonow
arxoft asked this question in Q&A
Discussion options

You must be logged in to vote

Greetings @arxoft ,

Yes, you can.

I'd also like to add that you can also use formatOptionLabel to render JSX in the output instead of passing in JSX in your options data. This provides the benefit of having more control of how it is rendered (menu option vs selected value option) as well as better separation of mapping data vs rendering logic.

const options = [
  { value: 323, item: "iPhone X", category: "Mobiles" },
  { value: 341, item: "MacBook Air", category: "Laptops" }
];

const formatOptionLabel = (opt, { context }) => {
  return (
    <span>
      {opt.item}&nbsp;
      <small>{opt.category}</small>
    </span>
  );
};

return <Select options={options} formatOptionLabel={formatOpt…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@arxoft
Comment options

Answer selected by arxoft
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants