0

I'm trying to output pretty fractions inside my options, ¼ ½ ¾ but it's displaying the "&frac 14;" as strings.

How can I get the JSX to interpret my optionContent and render the HTML, not as a string?

optionValue = "4.25" optionContent = "4 ¾" {optionContent}

enter image description here

1
  • can you show the code how you apply option in JSX ? Commented Jun 5, 2020 at 2:59

1 Answer 1

2

You can do it using the html-react-parser;

Example: https://codesandbox.io/s/parsing-html-react-zlmwj

import React, { useState } from "react";
import Parser from "html-react-parser";
import "./styles.css";

export default function App() {
  const [options, setOption] = useState(1);
  const [listOptions] = useState([
    { id: 1, desc: "4 ¼" },
    { id: 2, desc: "4 ½" },
    { id: 3, desc: "4 ¾" }
  ]);

  function handleSelect(e) {
    const selected = e.target.value;
    setOption(selected);
  }

  return (
    <div className="App">
      <h1>Example parsing html in React</h1>

      <select value={options} onChange={e => handleSelect(e)}>
        {listOptions.map(option => (
          <option key={option.id} value={option.id}>
            {Parser(option.desc)}
          </option>
        ))}
      </select>
    </div>
  );
}
Sign up to request clarification or add additional context in comments.

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.