Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

Warning: A component is contentEditable and contains children managed by React #53

Closed
iroy2000 opened this issue Feb 23, 2016 · 11 comments

Comments

@iroy2000
Copy link

The following is an example editor component

`
import React from 'react';
import {Editor, EditorState} from 'draft-js';

export default class MyEditor extends React.Component {

constructor(props) {
    super(props);

    this.state = {
        editorState: EditorState.createEmpty()
    };
}

onChange = (editorState) => {
    this.setState({editorState});
}

render() {
    const {editorState} = this.state;
    return <Editor editorState={editorState} onChange={this.onChange} />;
}

}
`

And in my parent component

import MyEditor from './MyEditor'

.... // in the render() method
`
render() {
let error = this.getError();

    return (
        <form className="post__form">
            {error}
            <div className="form-group">
                <label htmlFor="title">Title</label>
                <input type="text" ref="title" className="form-control" id="title" placeholder="Title" />
            </div>
            <div className="form-group">
                <label htmlFor="description">Description</label>

                <MyEditor />

            </div>
            <button type="submit" onClick={this.onClickHandler.bind(this)} className="btn btn-default">Submit</button>
        </form>
    );
}

`

And I see this warning

Warning: A component is contentEditable and contains children managed by React. It is now your responsibility to guarantee that none of those nodes are unexpectedly modified or duplicated. This is probably not intentional.

@tasti
Copy link
Contributor

tasti commented Feb 23, 2016

You aren't doing anything wrong.

This a known issue (http://facebook.github.io/draft-js/docs/advanced-topics-issues-and-pitfalls.html#react-contenteditable-warning) and it's being tracked in the react repo: facebook/react#5837

@hellendag
Copy link

Yep, really looking forward to eliminating this warning. :)

@davisml
Copy link

davisml commented Feb 24, 2016

I ended up adding this code to my project as a temporary solution:

console.error = (function() {
    var error = console.error

    return function(exception) {
        if ((exception + '').indexOf('Warning: A component is `contentEditable`') != 0) {
            error.apply(console, arguments)
        }
    }
})()

@jackcallister
Copy link

@davisml crazy, I was just looking for a way to do this.

@davisml
Copy link

davisml commented Feb 24, 2016

@jarsbe I'm glad I could help! :)

@jackcallister
Copy link

@davisml That's some speedy tech support, cheers!

mxstbr added a commit to mxstbr/draft-js that referenced this issue Feb 24, 2016
This will come into effect with the 15.0 release of React, see facebook/react#6112

Fixes facebookarchive#53
@sdoomz
Copy link

sdoomz commented Apr 30, 2016

As an alternative you can set your console filter to regexp mode with the value: ^((?!contentEditable).)*$

@Drafter500
Copy link

Drafter500 commented May 12, 2017

One more alternative is to add contentEditable attribute later on in componentDidMount() method. This produces no warning for me

  componentDidMount() {
     $(this.textedit).attr('contentEditable', true);
  }

  render() {
    const { value } = this.props;
    return (
      <div
        ref={(r) => { this.textedit = r; }}        
      >
        {value}
      </div>
    );
  }

@RTeran
Copy link

RTeran commented Apr 10, 2018

With the suppressContentEditableWarning prop you can solve this issue

@lazysergey
Copy link

lazysergey commented Apr 22, 2020

@RTeran this is real solution 👍

@nikhil-bacancy
Copy link

nikhil-bacancy commented Aug 4, 2020

<span id="activeLayerName" className="name" suppressContentEditableWarning={true} onClick={this.toggleLayerNameInEditMode} onBlur={this.toggleLayerNameInEditMode} contentEditable={this.state.isLayerNameEditable}> xyz </span>

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants