0

I have a JSON questions.json:

{
  "question1": "This is a <strong>cool</strong> question"
}

In my App.js I do:

import questions from './questions.json'

class App {
  render () {
    return (
        <p>{questions["question1"]}</p>
    )
  }

That displays:

"This is a < strong> cool < /strong> question"

(Notice I escaped "<\strong>" because SO would render "bool" bold otherwise.)

instead of

"This is a cool question"

I can think of writing a recursion that finds each "strong" markup, but I am wondering if a way already exists.

Thanks

1

2 Answers 2

1

Here's a working example using dangerouslySetInnerHTML.

const questions = {
  question1: 'This is a <strong>cool</strong> question'
};

class App extends React.Component {
  render () {
    return (
      <p dangerouslySetInnerHTML={{__html: questions['question1']}} />
    );
  }
}

// Render it
ReactDOM.render(
  <App />,
  document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>

Sign up to request clarification or add additional context in comments.

Comments

0

You can try using dangerouslySetInnerHTML and check if it works.

 <p dangerouslySetInnerHTML={{ __html: questions["question1"] }} />

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.