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>
user28
  • 467
  • 1
  • 8
  • 20
  • from where you are getting this `
    ` means is it coming in the `node.Comment` or from somewhere else?
    – Vikas Singh Apr 26 '19 at 08:26
  • yes i am getting it from node.Comment. To be clear, getrequest is sent to server from client, and as a response from server, client gets the json object. and client goes through this json object and gets the information of "Comment" . – user28 Apr 26 '19 at 08:30
  • then you need to set the `dangerouslySetInnerHTML` property. follow this link https://reactjs.org/docs/dom-elements.html – Vikas Singh Apr 26 '19 at 08:32
  • thank you everyone. – user28 Apr 26 '19 at 08:50
  • Description: 

    displayComment(){ var node = this.props.treeNode; return {__html: node.Comment};
    – user28 Apr 26 '19 at 08:50

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,893
  • 4
  • 17
  • 29
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,419
  • 13
  • 21
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' />
Avinash
  • 1,195
  • 9
  • 17