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

Add hacky workaround for getDerivedStateFromProps change in react 16.4 #25142

Merged
merged 3 commits into from
Nov 6, 2018
Merged
Changes from 1 commit
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: 40 additions & 0 deletions src/ui/public/query_bar/components/query_bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ interface State {
index: number | null;
suggestions: AutocompleteSuggestion[];
suggestionLimit: number;
stateChanged: boolean;
}

export class QueryBar extends Component<Props, State> {
public static getDerivedStateFromProps(nextProps: Props, prevState: State) {
if (prevState.stateChanged === true) {
return {
...prevState,
stateChanged: false,
};
}

if (nextProps.query.query !== prevState.query.query) {
return {
query: {
Expand Down Expand Up @@ -129,6 +137,7 @@ export class QueryBar extends Component<Props, State> {
index: null,
suggestions: [],
suggestionLimit: 50,
stateChanged: false,
};

public updateSuggestions = debounce(async () => {
Expand All @@ -143,6 +152,37 @@ export class QueryBar extends Component<Props, State> {
private componentIsUnmounting = false;
private persistedLog: PersistedLog | null = null;

/*
This is a really nasty hack required because React "fixed" getDerivedStateFromProps in version 16.4 [1].
We were relying on the fact that getDerivedStateFromProps was only called on prop change. The
reasons for this are outlined in the other comment above about avoiding triggering angular
digest cycles. I intentionally chose not to implement React's "Preferred Solution"[2] because it
would once again involve triggering angular digest cycles on every keypress. We can remove this hack
once this component is no longer used in Angular.

[1]: https://reactjs.org/blog/2018/05/23/react-v-16-4.html#bugfix-for-getderivedstatefromprops
[2]: https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#recommendation-fully-controlled-component
*/
public setState = <K extends keyof State>(
updater:
| ((prevState: Readonly<State>, props: Props) => Pick<State, K> | State)
| (Pick<State, K> | State),
callback?: () => void
) => {
if (typeof updater === 'function') {
return super.setState(
// TypeScript doesn't handle spread operators on generics well https://github.com/Microsoft/TypeScript/issues/13557
// @ts-ignore
(state, props) => ({ ...updater(state, props), stateChanged: true }),
callback
);
}
if (typeof updater === 'object') {
// @ts-ignore
return super.setState({ ...updater, stateChanged: true }, callback);
}
};

public isDirty = () => {
return this.state.query.query !== this.props.query.query;
};
Expand Down