Skip to content

Commit

Permalink
Fix lovasoa#161 by using PureComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
embiem committed Apr 18, 2019
1 parent fe7885b commit e7a4088
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 34 deletions.
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"prepare": "npm run compile"
},
"dependencies": {
"@types/prop-types": "^15.5.6",
"fast-deep-equal": "^2.0.1"
"@types/prop-types": "^15.5.6"
}
}
28 changes: 1 addition & 27 deletions src/react-contenteditable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import deepEqual from 'fast-deep-equal';
import * as PropTypes from 'prop-types';

function normalizeHtml(str: string): string {
Expand Down Expand Up @@ -35,7 +34,7 @@ function replaceCaret(el: HTMLElement) {
/**
* A simple component for an html element with editable contents.
*/
export default class ContentEditable extends React.Component<Props> {
export default class ContentEditable extends React.PureComponent<Props> {
lastHtml: string = this.props.html;
el: any = typeof this.props.innerRef === 'function' ? { current: null } : React.createRef<HTMLElement>();

Expand All @@ -62,31 +61,6 @@ export default class ContentEditable extends React.Component<Props> {
this.props.children);
}

shouldComponentUpdate(nextProps: Props): boolean {
const { props } = this;
const el = this.getEl();

// We need not rerender if the change of props simply reflects the user's edits.
// Rerendering in this case would make the cursor/caret jump

// Rerender if there is no element yet... (somehow?)
if (!el) return true;

// ...or if html really changed... (programmatically, not by user edit)
if (
normalizeHtml(nextProps.html) !== normalizeHtml(el.innerHTML)
) {
return true;
}

// Handle additional properties
return props.disabled !== nextProps.disabled ||
props.tagName !== nextProps.tagName ||
props.className !== nextProps.className ||
props.innerRef !== nextProps.innerRef ||
!deepEqual(props.style, nextProps.style);
}

componentDidUpdate() {
const el = this.getEl();
if (!el) return;
Expand Down

0 comments on commit e7a4088

Please sign in to comment.