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

Don't use setState for disabled KeyboardAvoidingView to avoid re-renders #38074

Conversation

adamgrzybowski
Copy link
Contributor

@adamgrzybowski adamgrzybowski commented Jun 26, 2023

Summary:

There are two reasons to apply these changes:

  • We don't need to re-render the KeyboardAvoidingView if it is disabled. It may be especially useful in combination with react-navigation where we could disable KeyboardAvoidingView for screens that are not focused
  • They fix the problem with the KeyboardAvoidingView wrapped inside the react-freeze component. Similarly, as above, it is useful when we want to freeze screens that are not visible for the user.

Changelog:

[GENERAL] [CHANGED] Don't use setState for disabled KeyboardAvoidingView to avoid re-renders

Test Plan:

  • Check if the KeyboardAvoidingView works as expected.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 26, 2023
@analysis-bot
Copy link

analysis-bot commented Jun 26, 2023

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 9,042,105 -3,890
android hermes armeabi-v7a 8,292,912 -2,359
android hermes x86 9,559,327 -2,847
android hermes x86_64 9,400,917 -3,607
android jsc arm64-v8a 9,601,195 -4,056
android jsc armeabi-v7a 8,729,303 -2,663
android jsc x86 9,689,260 -3,031
android jsc x86_64 9,934,903 -3,720

Base commit: 0bd6b28
Branch: main

@blakef
Copy link
Contributor

blakef commented Jun 27, 2023

Looks like you've got flow test failing, can you take a look?

_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.

@github-actions
Copy link

This PR is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 24, 2023
@adamgrzybowski
Copy link
Contributor Author

Is there anything else I should answer? @blakef @Pranav-yadav

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Jul 25, 2023
@efstathiosntonas
Copy link

Hi, what's the status on this PR? Is it ready to be merged?

@github-actions
Copy link

github-actions bot commented Sep 8, 2023

This PR is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days

@github-actions github-actions bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Sep 8, 2023
@adamgrzybowski
Copy link
Contributor Author

I think it still waits for approve

@github-actions github-actions bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Sep 9, 2023
@kelset
Copy link
Contributor

kelset commented Sep 11, 2023

given that checks are all passing, @cortinico maybe you can help find someone from Meta to get this in?

@facebook-github-bot
Copy link
Contributor

@blakef has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@blakef has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@github-actions
Copy link

This pull request was successfully merged by @adamgrzybowski in 783150f.

When will my fix make it into a release? | Upcoming Releases

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Sep 21, 2023
@facebook-github-bot
Copy link
Contributor

@blakef merged this pull request in 783150f.

ShevO27 pushed a commit to ShevO27/react-native that referenced this pull request Sep 26, 2023
…ers (facebook#38074)

Summary:
There are two reasons to apply these changes:
- We don't need to re-render the `KeyboardAvoidingView` if it is disabled. It may be especially useful in combination with [react-navigation](https://reactnavigation.org/) where we could disable `KeyboardAvoidingView` for screens that are not focused
- They fix the problem with the `KeyboardAvoidingView` wrapped inside the [react-freeze](https://github.com/software-mansion/react-freeze) component. Similarly, as above, it is useful when we want to freeze screens that are not visible for the user.

## Changelog:
[GENERAL] [CHANGED] Don't use setState for disabled KeyboardAvoidingView to avoid re-renders

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#38074

Test Plan: - Check if the KeyboardAvoidingView works as expected.

Reviewed By: sammy-SC

Differential Revision: D49148391

Pulled By: blakef

fbshipit-source-id: c4b7bde696d2249cbf4ad12c77058183b632464d
@toscalivia83
Copy link

Hello @adamgrzybowski,
I'm having an error in my RN 0.73.2 project, only in production on iOS:
on press on an Input, after displaying the keyboard, my app freezes.

I upgraded all the dependencies and it did not fix it.

No error is sent on focus to Sentry although I asked for one to be sent onFocus and onChangeText. I can't add anything with the keyboard. Although, in some cases, I can type something with the keyboard but it freezes 2 or 3 seconds later... I can't see any difference between when it works and when it does not.

This is my package.json, could you help me debug this? 🙏

{
  "name": "XXXX",
  "version": "2.3.2",
  "Android": {
    "versionCode": 57,
    "versionName": "2.3.2"
  },
  "private": true,
  "main": "index.js",
    "start": "react-native start",
    "test": "jest",
    "lint": "eslint .",
    "android:dev": "ENV=development ./envscript.sh && ENVFILE=.env.development && react-native run-android --mode=developmentdebug",
    "android:dev-release": "ENV=development ./envscript.sh && ENVFILE=.env.development && react-native run-android --mode=developmentrelease",
    "android:prod": "ENV=production ./envscript.sh && ENVFILE=.env.production && react-native run-android --mode=productiondebug",
    "android:prod-release": "ENV=production ./envscript.sh && ENVFILE=.env.production && react-native run-android --mode=productionrelease",
    "android:bundle-release": "ENV=production ./envscript.sh && cd android && npx react-native run-android --mode=ProductionRelease && ENVFILE=.env.production ./gradlew bundleRelease",
    "android:assemble-release": "ENV=production ./envscript.sh && cd android && ENVFILE=.env.production ./gradlew assembleRelease",
    "ios:dev": "ENV=development ./envscript.sh && ENVFILE=.env.development react-native run-ios",
    "ios:prod-release-on-my-device": "ENV=production ./envscript.sh && ENVFILE=.env.production react-native run-ios --device \"Marion's iPhone\""
  },
  "dependencies": {
    "@babel/runtime": "^7.23.8",
    "@bam.tech/react-native-image-resizer": "^3.0.7",
    "@notifee/react-native": "^7.8.2",
    "@react-native-async-storage/async-storage": "^1.15.9",
    "@react-native-clipboard/clipboard": "^1.11.2",
    "@react-native-community/datetimepicker": "6.7.3",
    "@react-native-firebase/analytics": "^18.7.3",
    "@react-native-firebase/app": "^18.7.3",
    "@react-native-firebase/auth": "^18.7.3",
    "@react-native-firebase/firestore": "^18.7.3",
    "@react-native-firebase/functions": "^18.7.3",
    "@react-native-firebase/messaging": "^18.7.3",
    "@react-native-firebase/storage": "^18.7.3",
    "@react-native-picker/picker": "^2.6.1",
    "@react-navigation/bottom-tabs": "^6.5.11",
    "@react-navigation/native": "^6.1.9",
    "@react-navigation/stack": "^6.3.20",
    "@stripe/stripe-react-native": "^0.35.1",
    "firebase": "^10.7.1",
    "react": "18.2.0",
    "react-native": "0.73.2",
    "react-native-app-intro-slider": "^4.0.4",
    "react-native-calendars": "^1.1284.0",
    "react-native-chart-kit": "^6.12.0",
    "react-native-config": "^1.4.5",
    "react-native-document-picker": "^8.1.3",
    "react-native-gesture-handler": "^1.10.3",
    "react-native-google-places-autocomplete": "^2.2.0",
    "react-native-image-crop-picker": "^0.40.2",
    "react-native-screens": "^3.29.0",
    "react-native-splash-screen": "^3.3.0",
    "react-native-vector-icons": "^10.0.3"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@react-native-picker/picker": "^2.2.1",
    "@react-native/babel-preset": "0.73.19",
    "@react-native/eslint-config": "0.73.2",
    "@react-native/metro-config": "0.73.3",
    "@react-native/typescript-config": "0.73.1",
    "@sentry/react-native": "^4.15.2",
    "@tanstack/query-async-storage-persister": "^4.29.5",
    "@tanstack/react-query": "^4.29.5",
    "@tanstack/react-query-persist-client": "^4.29.5",
    "@types/react": "^18.2.6",
    "@types/react-test-renderer": "^18.0.0",
    "AsyncStorage": "^0.1.5",
    "babel-jest": "^29.6.3",
    "dayjs": "^1.11.8",
    "eslint": "^8.19.0",
    "i18next": "^21.3.3",
    "jest": "^29.6.3",
    "moment": "^2.29.1",
    "numeral": "^2.0.6",
    "prettier": "2.8.8",
    "qs": "^6.10.4",
    "react-i18next": "^11.13.0",
    "react-native-image-crop-picker": "^0.39.0",
    "react-native-image-viewing": "^0.2.2",
    "react-native-localize": "^2.1.5",
    "react-native-maps": "^0.27.1",
    "react-native-modal-datetime-picker": "^13.1.2",
    "react-native-push-notification": "^8.1.1",
    "react-native-safe-area": "^0.5.1",
    "react-native-safe-area-context": "^3.3.2",
    "react-native-svg": "^13.9.0",
    "react-native-swipe-gestures": "^1.0.5",
    "react-native-web": "0.19.4",
    "react-native-webview": "^13.6.2",
    "react-test-renderer": "18.2.0",
    "typescript": "5.0.4"
  },
  "engines": {
    "node": ">=18"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Needs: Author Feedback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants