question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

contentEditable value is getting wiped out (question)

See original GitHub issue

I am not sure what I am doing wrong but certainly contentEditable value is getting reset on blur. Sorry for stupid question (I did search but did not find anything related to it.) also it is working in React set up but not in this setup

To reproduce JSfiddle https://jsfiddle.net/q4bbceg2/1/

problematic part of code is this

class ChildComponent extends Component {
 constructor() {
    super();
    this.state = {
      inputActive: false,
    };
  }
	render() {
   
		return ( <div
          role="button" style="border:1px solid red;"
          tabIndex="0"
          onFocus={() => { this.setState({ inputActive: true }); }}
          onBlur={() => { this.setState({ inputActive: false }); }}
          ref={(e) => { this.userInput = e; }}
       
          contentEditable="true"
          placeholder="Write a reply..."
          className="sc-user-input--text"
        >AAA
        </div>)
	}
}

I just wondering what I am missing here, if someone can point me in right direction it would be great.

Here is the same code in react https://jsfiddle.net/69z2wepo/173750/

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
yaodingydcommented, Apr 16, 2018

In react diffing is done with comparing two vnodes, so in react, state change triggers a rerender, but vnode has not changed, so no changes applies to DOM, thus your own change to DOM preserves.

But in preact diffing is done with DOM node reference and vnode. In rerender preact would think vnode has changed thus a update on DOM, which applies the original vnode to DOM.

1reaction
marvinhagemeistercommented, May 20, 2018

@paritoshmmmec The safest way to do it in both react and preact is to store the content inside the components state. Here is the fixed version of your fiddle: https://jsfiddle.net/q4bbceg2/4/

class ChildComponent extends Component {
  this.state = {
    value: "AAA",
  };

  render() { 
    return (
      <div
        onBlur={() => this.setState({
          // update text value
          value: this.userInput.textContent
        })}
        ref={(e) => { this.userInput = e; }}
        contentEditable="true"
      >
        {/* Always renders the correct value, now diffing works as expected */}
        {this.state.value}
      </div>
    );
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does React warn against an contentEditable component ...
The disadvantage would be that you might forget and get stuck wondering why user content is getting destroyed. No suppressing the error doesn't ......
Read more >
contenteditable - HTML: HyperText Markup Language | MDN
Chrome Edge contenteditable Full support. ChromeYes. Toggle history Full suppo... contenteditable="caret". Experimental Full support. ChromeYes. Toggle history Full suppo... contenteditable="events". Experimental Full support. ChromeYes. Toggle history...
Read more >
226941 - Contenteditable issues related to backspace handling
In DOM there is a <strong> element, then we are editing <strong> element, not bold text. The <strong> element has been removed from...
Read more >
Adjustable Form Text Area with the contenteditable Tag
A contenteditable div will keep the formatting and tags of whatever is pasted into it, which may be a headache if it needs...
Read more >
Fixing ContentEditable - Medium
That's pretty much the situation of contentEditable today. The difference here is that in order to fix it, first you need to convince...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found