Skip to content
Closed
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
_subscriptions: Array<EventSubscription> = [];
viewRef: {current: React.ElementRef<typeof View> | null, ...};
_initialFrameHeight: number = 0;
_bottom: number = 0;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -129,20 +130,32 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
}
};

// Avoid unnecessary renders if the KeyboardAvoidingView is disabled.
_setBottom = (value: number) => {
const {enabled = true} = this.props;
this._bottom = value;
if (enabled === true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (enabled === true) {
if (enabled) {

Self explanatory, though stylistic :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@adamgrzybowski I see you made that change on your 2nd commit. What was your thinking?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is why the flow tests were failing. For some reason if(enabled) is not enough with const {enabled = true} = this.props

Sketchy null check on boolean [1] which is potentially false. Perhaps you meant to check for null or undefined [2]?
[sketchy-null-bool]

 [2][1]  45│   enabled?: ?boolean,
           :
        134│   _setBottom = (value: number) => {
        135│     const {enabled = true} = this.props;
        136│     this._bottom = value;
        137│     if (enabled) {
        138│       this.setState({bottom: value});
        139│     }
        140│   };

But to be honest I am not convinced if this is the right way.

I just checked different approach and it works fine for

const enabled = this.props.enabled ?? true;
if (enabled) { ... }

I could switch to this syntax to make it more readable. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this.setState({bottom: value});
}
};

_updateBottomIfNecessary = async () => {
if (this._keyboardEvent == null) {
this.setState({bottom: 0});
this._setBottom(0);
return;
}

const {duration, easing, endCoordinates} = this._keyboardEvent;
const height = await this._relativeKeyboardHeight(endCoordinates);

if (this.state.bottom === height) {
if (this._bottom === height) {
return;
}

if (duration && easing) {
this._setBottom(height);

const {enabled = true} = this.props;
if (enabled === true && duration && easing) {
LayoutAnimation.configureNext({
// We have to pass the duration equal to minimal accepted duration defined here: RCTLayoutAnimation.m
duration: duration > 10 ? duration : 10,
Expand All @@ -152,9 +165,15 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
},
});
}
this.setState({bottom: height});
};

componentDidUpdate(_: Props, prevState: State): void {
const {enabled = true} = this.props;
if (enabled === true && this._bottom !== prevState.bottom) {
this.setState({bottom: this._bottom});
}
}

componentDidMount(): void {
if (Platform.OS === 'ios') {
this._subscriptions = [
Expand Down