Zirek
Zirek

Reputation: 523

React - put html tags inside json content

I'm trying to make multilingual website with redux, I have followed this tutorial > http://www.bebetterdeveloper.com/coding/getting-started-react-redux.html and adapt it to my project however I have a problem. I put my whole content inside json file. The thing is I wish to make some particular words bolded. Is it possible to add html tags inside the Json content? I found some way in the google but it doesn't work... Will be glad for any suggestions, That's my code:

[
    {
      "lang": "en",
      "page": {
        "menu": {
          "home": "Home",
          "brand": "Brand",
          "contact": "Contact"
        },
        "home": {
          "header": "Lorem Ipsum",
          "paragraphOne": "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...",
          "paragraphTwo": "Lorem <span className=\"bold-me\"> ipsum <\/span> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        }
      }
    }
  }
]

Upvotes: 1

Views: 475

Answers (1)

Tholle
Tholle

Reputation: 112777

You can use dangerouslySetInnerHTML if you want to parse the HTML in a string. You could then style the bold-me class to have bolder text, or simply use the b tag.

Example

const content = [
  {
    lang: "en",
    page: {
      menu: {
        home: "Home",
        brand: "Brand",
        contact: "Contact"
      },
      home: {
        header: "Lorem Ipsum",
        paragraphOne:
          "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit...",
        paragraphTwo:
          "Lorem <b> ipsum </b> dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
      }
    }
  }
];

function App() {
  return (
    <div
      dangerouslySetInnerHTML={{ __html: content[0].page.home.paragraphTwo }}
    />
  );
}

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

Upvotes: 1

Related Questions