Giter VIP home page Giter VIP logo

Comments (4)

ankri avatar ankri commented on May 26, 2024

You would basically update the prop on every keypress. I would suggest to either debounce the function or move setProp... to onBlur.

from craft.js.

chungchi300 avatar chungchi300 commented on May 26, 2024

I totally agree that I am re-rendering every time, but I am just wondering why

<div>
            <EditButton icon={<FormatItalicIcon />} cmd="italic" />
            <EditButton icon={<FormatBoldIcon />} cmd="bold" />

            <EditButton
              cmd="createLink"
              arg="https://github.com/lovasoa/react-contenteditable"
              icon={<LinkIcon />}
            />
            <ContentEditable
              html={value} // innerHTML of the editable div
              onChange={(e) => {
                setProp((prop) =>
                  setProp((props) => {
                    props[propKey] = onChange
                      ? onChange(e.target.value)
                      : e.target.value;
                  })
                );
              }} // use true to disable editing
              style={{
                margin: "27px",
                border: "1px solid lightgray",
              }}
            />
          </div>

works good but not RichTextEditor

from craft.js.

chungchi300 avatar chungchi300 commented on May 26, 2024

my latter workable fixs

class SmartRichTextEditor extends React.Component {
  state = {
    value: undefined,
  };

  shouldComponentUpdate(nextProps, nextState) {
    if (
      this.props.value ==
      RichTextEditor.createValueFromString(nextProps.html, "html")
    ) {
      return false;
    }
    return true;
  }
  render() {
    console.log("rendering new");
    return (
      <RichTextEditor
        value={
          this.state.value
            ? this.state.value
            : RichTextEditor.createValueFromString(this.props.html, "html")
        }
        onChange={(value) => {
          this.setState({ value });

          this.props.onChange(value.toString("html"));
        }} // use true to disable editing
        style={{
          margin: "27px",
          border: "1px solid lightgray",
        }}
      />
    );
  }
}

from craft.js.

prevwong avatar prevwong commented on May 26, 2024

@chungchi300 I'm not familiar with react-rte but typically when a prop passed to a React element changes, it causes the element to re-render under normal circumstances. In the case of a content-editable element that consumes a prop that changes on every key press, it will cause the element to re-render and lose its cursor position.

react-contenteditable works because it uses a pretty similar workaround as your solution above 💯

from craft.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.