Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] fix(TagInput): prevent infinite setState in componentDidUpdate #3811

Merged
merged 3 commits into from
Nov 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions packages/core/src/components/tag-input/tagInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,14 @@ export interface ITagInputState {
activeIndex: number;
inputValue: string;
isInputFocused: boolean;
}

export interface ITagInputSnapshot {
inputValue: string;
prevInputValueProp?: string;
}

/** special value for absence of active tag */
const NONE = -1;

@polyfill
export class TagInput extends AbstractPureComponent2<ITagInputProps, ITagInputState, ITagInputSnapshot> {
export class TagInput extends AbstractPureComponent2<ITagInputProps, ITagInputState> {
public static displayName = `${DISPLAYNAME_PREFIX}.TagInput`;

public static defaultProps: Partial<ITagInputProps> & object = {
Expand All @@ -211,6 +208,19 @@ export class TagInput extends AbstractPureComponent2<ITagInputProps, ITagInputSt
tagProps: {},
};

public static getDerivedStateFromProps(
props: Readonly<ITagInputProps>,
state: Readonly<ITagInputState>,
): Partial<ITagInputState> | null {
if (props.inputValue !== state.prevInputValueProp) {
return {
inputValue: props.inputValue,
prevInputValueProp: props.inputValue,
};
}
return null;
}

public state: ITagInputState = {
activeIndex: NONE,
inputValue: this.props.inputValue || "",
Expand All @@ -225,16 +235,18 @@ export class TagInput extends AbstractPureComponent2<ITagInputProps, ITagInputSt
},
};

public getSnapshotBeforeUpdate(prevProps: Readonly<ITagInputProps>): ITagInputSnapshot {
return {
inputValue: prevProps.inputValue !== this.props.inputValue ? this.props.inputValue : this.state.inputValue,
};
}
// public getSnapshotBeforeUpdate(prevProps: Readonly<ITagInputProps>): ITagInputSnapshot | null {
// if (prevProps.inputValue !== this.props.inputValue) {
// return { inputValue: this.props.inputValue };
// }

public componentDidUpdate(_: ITagInputProps, __: ITagInputState, snapshot: ITagInputSnapshot) {
super.componentDidUpdate(_, __, snapshot);
this.setState(snapshot);
}
// return null;
// }

// public componentDidUpdate(props: ITagInputProps, state: ITagInputState, snapshot: ITagInputSnapshot) {
// super.componentDidUpdate(props, state, snapshot);
// this.setState(snapshot);
// }

public render() {
const { className, disabled, fill, inputProps, intent, large, leftIcon, placeholder, values } = this.props;
Expand Down