0

I am setting up an application and At GUI side, I would like to display the textcomment in different lines rather than in one single line. currently it works as follows: 1. From backend json file is received which has key:value pair for comment. Example-> Comment: "Application Entity Title for Service.
The sending system must use exactly the same name to send data to service.

  1. At GUI side, after parsing the JSON file, GUI tries to display the comment.

Issue: GUI is not recognizing the HTML tag
and not displaying a line break.

Question: do I need to handle it in the backend(in C#) in a different way?

json received from backend:

Comment: "Application Entity Title for Service.<br/> The sending system must use exactly the same name to send data to service. <br/>"

GUI react code

<div className="Setting__Comment">
  <p>
    <strong>Description:&nbsp;</strong>
     {node.Comment}
  </p>
</div>

Current Result at GUI side

Description: Application Entity Title for Service.<br/> The sending system must use exactly the same name to send data to service. <br/>

Expected Results.

Description: Application Entity Title for Service. 
             The sending system must use exactly the same name to send data to 
             service.

Solution which worked for me:

  displayComment(){
    var node = this.props.treeNode;
    return {__html: node.Comment};

            <div className="SettingsTreeValueNode__Comment">
              <p>
                <strong>Description:&nbsp;</strong>
                <div dangerouslySetInnerHTML={this.displayComment()}/>
              </p>
            </div>
          </div>
hightides1973
  • 497
  • 1
  • 9
  • 27

4 Answers4

1

You'd better break down your text into render-able blocks. if you have a <br> in your message, then it should be handled separately as an html token, not a text. so, i'd recommend return an array from your service and for br tokens just render a br explicitly.

Alternatively, you can use dangerouslySetInnerHTML to render the content as is, but it's not the standard way and should be avoided as much as possible.

Siavash Rostami
  • 1,883
  • 4
  • 17
  • 31
1

You can use

    <div className="Setting__Comment">
     <p>
       <strong>Description:&nbsp;</strong>
       <div dangerouslySetInnerHTML={__html: node.Comment} />
     </p>
    </div>
1

try this way

<div className="Setting__Comment">
     <p>
       <strong>Description:&nbsp;</strong>
       <div dangerouslySetInnerHTML={{__html : node.Comment}} />
     </p>
    </div>
Vikas Singh
  • 1,791
  • 1
  • 14
  • 26
0

First, split('<br/>') the string by <br/> and convert to Array.
Then, In React component, Iterate or use indexes to display array values:-

class App extends React.Component{
 render(){
 var Comment = "Application Entity Title for Service.<br/> The sending system must use exactly the same    name to send data to service. <br/>";
  var array = Comment.split('<br/>');
  // console.log(array);
  return(
    <div>
    <div className="Setting__Comment">
         <p>
           <strong>Description:&nbsp;</strong>
            {array.map(comment => <React.Fragment>{comment}<br/></React.Fragment>)
            }
         </p>    
     </div>
    </div>  )
 }
}

ReactDOM.render(<App/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root' />
Engineer
  • 1,243
  • 11
  • 18