0

I am creating a react app and getting all data from API calls. I have set data into an array and need to display them. My data set is like below.

[{idappcontent: "Sign_01", content: "<div style=\"color: #0D0D0D;\"> When you give us your name,<br /> e-mail address and the dates of your birthday </div>"}
{idappcontent: "Sign_02", content: "<div style=\"text-align:center\"> <label for=\"firstName\">First Name</label></div><div style=\"margin-top: 3%;\"><input id=\"fname\" name=\"firstName\" onkeyup=\"FirstnameValidation()"/></div>"}
{idappcontent: "Sign_03", content: "<div style=\"text-align:center\"> <label for=\"lname\">Last Name</label> </div><div style=\"margin-top: 3%;\"><input id=\"lname\" name=\"lname\" style=\"font-family: trajanPro;width:10 /></div>"}
{idappcontent: "Sign_04", content: "<div style=\"text-align:center\"><label for=\"email\">Email Address</label></div><div style=\"margin-top: 1%;\"><input id=\"email\" type=\"email\" name=\"email\" onkeyup=\"emailvalidat()"/></div>"} ]

How can I display "content" data in react js?

How to display them inside div tag like below

<div class="Header-banner" style="text-align: center;" id="Sign_01"></div>
    <div id="Sign_02"></div>
    <div id="signUpfrm" style="width:100%">
        <div style="text-align:center">
            <div style="display:inline-table;width:100%">
                <div id="Sign_03" style="display: inline-block; text-align: right; width: 32%;margin-right: 1px;"></div>
                <div id="Sign_04" style="display: inline-block; text-align: left; width: 32%; margin-left: 2px;"></div>
            </div>
            <div id="Sign_05" style="margin-top: 2%; width: 65%; display: inline-table;"></div>

            <div id="Sign_09" class="MainWrapper" style="margin-top: 2%; width: 65%; display: inline-table;"></div>
            <div style="display:inline-flex; width: 89%;">
                <div id="Sign_07" class="MainWrapper" style="width:50%"></div>
                <div id="Sign_08" class="Mainwrapper" style="width: 50%; margin-top: 8px;"></div>
            </div>
            <div style="height: 28px; margin-top: 4%">
                <div id="Sign_06" class="MainWrapper" style="margin-top: 4%;"></div>
            </div>

        </div>
    </div>
1
  • 1
    Even though there is already an answer here, I'll still ask what you've tried already on your own to render any of the array you've shared, and to describe what that the issue is, if there is one. stackoverflow.com/help/minimal-reproducible-example Commented Aug 9, 2021 at 5:15

1 Answer 1

3

You should use map function and dangerouslySetInnerHTML for the content part:

import DOMPurify from 'dompurify';

return (
  arr.map((item, index) => (
  <div key={item.idappcontent}>
    <div>
      {item.idappcontent}
    </div>
    <div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(item.content) }}/>
  </div>
  ))
)
Sign up to request clarification or add additional context in comments.

8 Comments

You can use DOMPurify.sanitize(content) before setting it in dangerouslySetInnerHTML to avoid XSS attack. It will make this solution a perfect solution.
could you please add a sample code? I'm new to react.
I have implemented the solution, please check here: stackblitz.com/edit/react-ec9c8v?file=src/App.js
@AnilKumar Good habit to be in, but if OP is getting injection attacks from their own APIs then I would assume a more egregious compromise has already occurred. That's pretty much game over. 😆
@DrewReese I agree with your point but in every enterprise apps these issues are highlighted in pen testing/Fortify irrespective of being own API. We cannot predict which layer will compromised. Response can be intercepted via tools such as fiddler etc and response can be modified with script tags to redirect user to third party page.
|

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.