React dangerous SetInnerHtml not working under certain conditions

I use dangerouslySetInnerHtmlto insert links into a component of the reaction. The method rendercalls a method rowValuethat checks the state of the component and returns the appropriate markup for rendering.

This is the method rowValue:

rowValue: function() {
    var fieldValue;

    if(!this.state.isBeingEdited) {
        fieldValue = (
            <div dangerouslySetInnerHtml={{ __html: this.props.value }} />
        );
    } else {
        fieldValue = (
            <div className="col-sm-6 col-xs-6 js-field-wrapper">
                <input type="text"
                       defaultValue={this.extractUrl(this.props.value)}
                       className="form-control" />
            </div>
        );
    }

    return fieldValue;
},

At initial rendering, the contents of the div are empty:

<div class="col-sm-6 col-xs-6" data-reactid=".1.1.0.0.0.1.0.0.2.1:$angellist.0.1"></div>

But when the editvalue is set in the component state true, the input is correctly filled with the correct value:

<div class="col-sm-6 col-xs-6 js-field-wrapper" data-reactid=".1.1.0.0.0.1.0.0.2.1:$angellist.0.1">
    <input type="text" class="form-control" value="https://example.com" data-reactid=".1.1.0.0.0.1.0.0.2.1:$angellist.0.1.0">
</div>

Setting the state editback to false displays an empty div.

Flowing divwith dangerouslySetInnerHtmlprop inside another divdoes not change anything.

+4
source share
1

, , . , :

div . , dangerouslySetInnerHtml, URL- href .

  fieldValue = (
      <div className="col-sm-6 col-xs-6">
         <a href="{this.props.url}">{this.props.url}</a>
      </div>
    );
+2

Source: https://habr.com/ru/post/1609251/


All Articles