reactjs - dangerouslySetInnerHtml doesn't update during render -
so made component including content-editable components in app. copied gist believe, edited needed.
the code below. when edit it, triggers updates on parent fine, when attempt set props.html in parent, doesn't reflect in ui. further, console.log shows this.props.html equal '' blank string, yet ui doesn't update, , maintains text in there.
i don't understand how possible... dangerouslysetinnerhtml = {__html: ''} should make ui reflects empty string... feels should impossible show old text.
var react = require('react'); var contenteditable = react.createclass({ render: function(){ //todo: find html=undefined , fix it! can remove this? maybe should keep safety. var html = this.props.html || ''; console.log('content editable render, html: ', this.props.html); return <div id="contenteditable" oninput={this.emitchange} onblur={this.emitchange} contenteditable dangerouslysetinnerhtml={{__html: html}}></div>; }, shouldcomponentupdate: function(nextprops){ return nextprops.html !== this.getdomnode().innerhtml; }, emitchange: function(){ var html = this.getdomnode().innerhtml; if (this.props.onchange && html !== this.lasthtml) { this.props.onchange({ target: { value: html } }); } this.lasthtml = html; } }); module.exports = contenteditable; (a little background, i'm trying clear input after submitting saved. clearing isn't working, hence question.)
i got similar problem using contenteditable , shouldcomponentupdate, looks there bug when resetting innerhtml same previous value using dangerouslysetinnerhtml function (or prop) (i think not work if insert code without using it) ... suspect (this idea) react compares last value set through dangerouslysetinnerhtml new 1 trying send , decides not update component because same (even if real innerhtml has changed due user interactions, because interactions not trigger state or props update on react).
solution: easiest solution found use different key each time needed re-render. example using key={date()}.
example: here can find code (i changed of make work), when type '?' div, text inside contenteditable component should become empty string (i.e. ''), works once, second time type '?' won't work because innerhtml react same 1 you're setting (i.e. empty string won't update component).
and here, added key={date()} (this easiest way show work, not best way set unique key each time re-render) editable component, can type number of '?' , work.
Comments
Post a Comment