Skip to content

RadioGroup - Replace unsafe method with getDerivedStateFromProps #932

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

Merged
merged 1 commit into from
Sep 15, 2020
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
37 changes: 30 additions & 7 deletions src/components/radioButton/RadioGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import _ from 'lodash';
import React, {PureComponent} from 'react';
import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjectedProps} from '../../commons/new';
import React, {PureComponent, GetDerivedStateFromProps} from 'react';
import {
asBaseComponent,
forwardRef,
BaseComponentInjectedProps,
ForwardRefInjectedProps
} from '../../commons/new';
import View from '../view';
import RadioGroupContext from './RadioGroupContext';

Expand All @@ -19,7 +24,9 @@ interface RadioGroupState {
value?: RadioGroupPropTypes['initialValue'];
}

type Props = RadioGroupPropTypes & BaseComponentInjectedProps & ForwardRefInjectedProps;
type Props = RadioGroupPropTypes &
BaseComponentInjectedProps &
ForwardRefInjectedProps;

/**
* Wrap a group of Radio Buttons to automatically control their selection
Expand All @@ -35,10 +42,24 @@ class RadioGroup extends PureComponent<Props, RadioGroupState> {
};
}

UNSAFE_componentWillReceiveProps(nextProps: Props) {
if (this.props.initialValue !== nextProps.initialValue) {
this.setState({value: nextProps.initialValue});
static getUpdatedState = (
nextProps: Props,
prevState: RadioGroupState
): RadioGroupState | null => {
const {value} = prevState;
const {initialValue} = nextProps;

if (_.isUndefined(nextProps.initialValue) || value === initialValue) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why no turn the component to uncontrolled component? Where the initialValue is literally initial value and not updated through a prop change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Inbal-Tish, Yeah, I though about it, but then we going to lose the use case to let users 'reset' the component value.
I consult with Ethan to have a second opinion and we said that this use-cases should be kept

return null;
}

return {
value: initialValue
}
};

static getDerivedStateFromProps: GetDerivedStateFromProps<Props, RadioGroupState> = (props, state) => {
return RadioGroup.getUpdatedState(props, state);
}

getContextProviderValue() {
Expand All @@ -62,4 +83,6 @@ class RadioGroup extends PureComponent<Props, RadioGroupState> {
}
}

export default asBaseComponent<RadioGroupPropTypes>(forwardRef(RadioGroup));
export {RadioGroup}; // For tests

export default asBaseComponent(forwardRef(RadioGroup));