JavaScript trying to add br tag between the text in react

I am trying to add br tag between the text in react. can you guys tell me how to add it. I tried adding br tag... but if I add I get
tag in the browser. providng my code below. can you guys tell me how to fix it.

let sportsDescription = '';

        if(sportsInfo !== '' && !isSportsSetupActive) {
            sportsDescription = 'testing <br /> testing';

        }else if(isSportsSetupActive) {
            sportsDescription = 'testing <br /> testing';

        }



        return (
            <div id="main" className="section flex-container flex-full">
                <section className="page-content flex-item">
                    <section className="gray-box">
                        <h2>Welcome to your  dashboard{formatName}.</h2>
                        <p className="ft-intro ft-day1 ft-day2">{sportsDescription}</p>

                    </section>
                </section>
            </div>
    );
Answer:1

From the ReactJS Docs...

In general, setting HTML from code is risky because it's easy to inadvertently expose your users to a cross-site scripting (XSS) attack. So, you can set HTML directly from React, but you have to type out dangerouslySetInnerHTML and pass an object with a __html key, to remind yourself that it's dangerous.

In your example, {sportsDescription} is interpreted as a string. React won't see anything inside that variable as a DOM element. That's why your <br/> only renders as a string.

Alternatively, you could do this (but it's generally a bad idea)...

 <p 
   className="ft-intro ft-day1 ft-day2" 
   dangerouslySetInnerHTML={{__html:sportsDescription}} 
 />

This way, React correctly recognized your intent to include HTML inside the string sportsDescription and renders the desired <br/> element.

Here's a quote from an article that goes into greater detail about Getting Started with ReactJS:

The React module has a createClass method that takes an object as its only parameter. We define only one key to start with: render. The value of render is a function that returns a virtual DOM element. One of the keys to React’s performance is its virtual DOM abstraction. Essentially, it’s a difference engine that calculates the difference between the existing DOM and the DOM that is to be rendered and only modifies the necessary elements and attributes. Inside the render function, you may have noticed that I included HTML markup. That’s JSX. It won’t pass validation as legal Javascript. Instead, it requires the Babel transpiler.

Answer:2

I wrote a simple code to choose random name from four, using "random_word();" function and it doesn't work. It says:"undefined". Can anyone explain me why and help to fix it? Thanks for all help. var ...

I wrote a simple code to choose random name from four, using "random_word();" function and it doesn't work. It says:"undefined". Can anyone explain me why and help to fix it? Thanks for all help. var ...

  1. javascript function returning undefined
  2. javascript function returning function
  3. javascript function returning multiple values
  4. javascript function returning value
  5. javascript function returning boolean
  6. javascript function returning object
  7. javascript function returning promise
  8. javascript function returning array
  9. javascript function returning nan
  10. javascript function returning true or false
  11. javascript function returning another function
  12. javascript function returning two values
  13. javascript function returning 2 values
  14. javascript function returning int
  15. javascript function returning json
  16. javascript function returning a boolean value
  17. javascript function returning json object
  18. javascript function returning html
  19. javascript function returning undefined value
  20. javascript function returning integer

I'm having some trouble with using Dropzone.js within a standard html form. With the code I have, everything is working as it should, I can click the area to add an image, I can only add one image at ...

I'm having some trouble with using Dropzone.js within a standard html form. With the code I have, everything is working as it should, I can click the area to add an image, I can only add one image at ...

  1. dropzone form submit
  2. dropzone submit form with files
  3. dropzone submit form without files
  4. dropzone submit form data
  5. dropzone js form submit
  6. dropzone processqueue on form submit
  7. dropzone upload on form submit
  8. dropzone upload file on form submit
  9. dropzone js upload on form submit

<div contenteditable="true"> abcde<img src="abc.jp" />fg </div> $('div').append('<img src="abc.jpg" />'); //this will append img at end of text I try to add an image into ...

<div contenteditable="true"> abcde<img src="abc.jp" />fg </div> $('div').append('<img src="abc.jpg" />'); //this will append img at end of text I try to add an image into ...

  1. jquery append string to string
  2. jquery append string as html
  3. jquery append string in loop
  4. jquery append string with comma
  5. jquery append string to input value
  6. jquery append string to url
  7. jquery append string to element
  8. jquery append string to href
  9. jquery append string to class name
  10. jquery append string to array
  11. jquery append string to id
  12. jquery append string not html
  13. jquery append string to textarea
  14. jquery append string to body
  15. jquery append string to textbox
  16. jquery append string before
  17. jquery append string to var
  18. jquery append string text
  19. jquery append string object
  20. jquery append string position

Hi I noticed a very interesting behavior in JavaScript RegEx.exec function. if I run the same regex multiple times on a string that returns positive, it will return positive only half of the time.. (...

Hi I noticed a very interesting behavior in JavaScript RegEx.exec function. if I run the same regex multiple times on a string that returns positive, it will return positive only half of the time.. (...