A framework for building native macOS apps with React.
Перейти к файлу
Eli White e9b4928311 TextInput: Don't do an extra round trip to native on focus/blur
Summary:
I wrote up a bunch of context for this in response to #27038 by fat. That comment is reproduced here in this commit message. You can see it in it's original contxt here: https://github.com/facebook/react-native/pull/27038

Okay, here is what I think is happening. For context, here is a diagram I have of how focus and blur propagates through the system. This might be interesting to refer back to as you go through the rest of my explanation.

![graphviz (12)](https://user-images.githubusercontent.com/249164/67992345-982c9d80-fbf9-11e9-96ea-b091210dddbe.png)

ScrollView's scrollResponder is responsible for blurring text inputs when a touch occurs in the ScrollView but outside of the currently focused TextInput. The code for that is here:
6ba2769f0f/Libraries/Components/ScrollResponder.js (L301-L314)

This happens on `scrollResponderHandleResponderRelease` aka, touch up.

It checks for what the currently focused textinput is by calling `TextInputState.currentlyFocusedField()`.

That function is a JS variable that is being updated by calls to `TextInputState.focusTextInput` and `TextInputState.blurTextInput`:

6ba2769f0f/Libraries/Components/TextInput/TextInputState.js (L36-L71)

I added some console logs to those methods to see which ones are being called when running your repro (thanks for the repro!). **This is without your fix**

Click on and off:
```
// Click on input 1
focusTextInput input1
TextInput's _onFocus called

// Click on blank space
scrollResponderHandleResponderRelease blur input1
blurTextInput input1
TextInput's _onBlur called
```

Click on input1, then input 2, then off
```
// Click on input 1
focusTextInput input1
TextInput's _onFocus called for input1

// Click on input 2
focusTextInput input2
TextInput's _onBlur called for input1
TextInput's _onFocus called for input2

// Click on blank space
scrollResponderHandleResponderRelease blur input2
blurTextInput input2
TextInput's _onBlur called for input2
```

And now for the bug. Click on input 1, tab to 2, then off
```
// Click on input 1
focusTextInput input1
TextInput's _onFocus called for input1

// Tab to input 2
TextInput's _onBlur called for input1
TextInput's _onFocus called for input2

// Click on blank space
scrollResponderHandleResponderRelease blur input1
blurTextInput input1
```

Notice how `focusTextInput` was never called with input2 in the last example. Since this is the function that sets the `currentlyFocusedField` when we click on the blank space RN is trying to blur the first input instead of the second.

# The root cause
We are tracking the state of which field is focused in JS which has to stay in sync with what native knows is focused. We [listen to _onPress](6ba2769f0f/Libraries/Components/TextInput/TextInput.js (L1103-L1107)) and call `TextInputState.focusTextInput` in that handler. However, we don't currently have anything listening to other ways for an input to become focused (like tabbing) so it doesn't end up updating the `currentlyFocusedField`.

We have the same problem with blur that we actually fixed the same way you did here in this PR:
6ba2769f0f/Libraries/Components/TextInput/TextInput.js (L1182-L1189)

If you look back at my diagram at the beginning of this post, you'll notice the missing edge from `TextInput._onFocus` to `TextInputState.focusTextInput`. That's the problem. :)

The reason this solution works is because this function **is** the notification from native that an input was focused or blurred. This solution is *fine* because this updates the `currentlyFocusedID` but isn't great because it both sets that value and **calls the native code to focus or blur again**. Luckily the native code doesn't send an event back to JS if you try to blur an already blurred TextInput otherwise we'd have an infinite loop.

# The correct solution
The correct thing would probably be to have all of this tracking in native code and not in JavaScript code. That's a pretty big change though and very out of scope. Something for our team to keep in mind for the future.

A short term term solution would be to refactor `focusTextInput` and `blurTextInput` to pull out the part that sets the `currentlyFocusedID` that we could call from `TextInput` directly from `_onFocus` and `_onBlur`.

# ^This short term term solution is what this commit is doing.

Changelog:
[General][Changed] TextInput no longer does an extra round trip to native on focus/blur

Reviewed By: RSNara

Differential Revision: D18278359

fbshipit-source-id: 417566f25075a847b0f4bac2888f92fbac934096
2019-11-04 14:42:44 -08:00
.appveyor Bump Android NDK to r19c (#25140) 2019-06-04 13:59:36 -07:00
.circleci Tidy up license headers [3/n] 2019-10-16 10:06:34 -07:00
.github Add Landcastle job that requires changelog for React Native diffs 2019-10-11 15:25:00 -07:00
IntegrationTests Update Broken Link #devcjog25 (#27025) 2019-10-28 12:11:45 -07:00
Libraries TextInput: Don't do an extra round trip to native on focus/blur 2019-11-04 14:42:44 -08:00
RNTester Refactor RNTester SliderExample to use Hooks 2019-11-04 12:16:37 -08:00
React Remove -Wno-unguarded-availability from ReactInternal 2019-11-04 12:03:10 -08:00
ReactAndroid Fix Collapsing of Delete-Create mounting instructions 2019-11-04 14:34:33 -08:00
ReactCommon Remove unneeded NSNotification center removeObserver 2019-11-04 10:19:30 -08:00
bots Bots cleanup, avoid leaving inline reviews when N>5 (#24923) 2019-05-21 19:38:54 -07:00
docs Generate doc info for RN components 2019-09-19 18:36:25 -07:00
flow Update Broken Link #devcjog25 (#27025) 2019-10-28 12:11:45 -07:00
flow-typed/npm remove deprecated utilities 2019-01-15 13:59:31 -08:00
gradle/wrapper Update Gradle wrapper to 5.6.3 (#26915) 2019-10-21 13:30:52 -07:00
jest Animated: Delete `__skipSetNativeProps_FOR_TESTS_ONLY` 2019-11-03 11:59:53 -08:00
keystores Use fb_native_wrapper for all targets 2018-10-31 11:47:42 -07:00
local-cli Update references to the CLI (#23052) 2019-01-21 09:13:08 -08:00
packages Export native commands codegen classes and fix build issue 2019-11-04 11:17:42 -08:00
scripts Update iOS Simulator OS version to 12.4 2019-10-29 18:10:58 -07:00
template Updated Jitpack URL in android template to fix gradle errors (#26660) 2019-10-28 16:10:55 -07:00
third-party-podspecs TM iOS: Set up CocoaPods specs for the TM specs 2019-07-01 15:20:01 -07:00
tools/build_defs Combine rn_debug_flags and rn_feature_flags 2019-09-12 13:09:29 -07:00
.buckconfig Update Broken Link #devcjog25 (#27025) 2019-10-28 12:11:45 -07:00
.buckjavaargs limiting BUCK's memory for CI 2016-02-01 10:49:33 -08:00
.clang-format Moving ObjC specific clang-format rules to the common config 2019-02-11 13:07:09 -08:00
.editorconfig https link to EditorConfig.org (#25267) 2019-06-18 01:58:07 -07:00
.eslintignore Eliminate eslint npm version mismatch warnings and bump some to latest (#23969) 2019-03-19 11:14:30 -07:00
.eslintrc Move error-subclass-name lint rule to GitHub 2019-10-09 11:48:07 -07:00
.flowconfig Enable abstract locations for 50% of users in xplat/js 2019-10-28 13:24:11 -07:00
.flowconfig.android Enable abstract locations for 50% of users in xplat/js 2019-10-28 13:24:11 -07:00
.gitattributes Added a .gitattributes file, ensuring that Bash script source files (gradlew and 2015-10-13 23:10:39 -04:00
.gitignore iOS Pods: use tar.gz offline mirrors instead of pods replica 2019-09-20 16:20:16 -07:00
.nvmrc Add .nvmrc + fix node version for eslint 5 compat (#20109) 2018-07-31 02:34:16 -07:00
.prettierrc Set the Prettier config so it is not forced on users of @react-native-community/eslint-config (#24635) 2019-04-29 09:44:51 -07:00
CODE_OF_CONDUCT.md Adopt Contributor Covenant 2019-08-29 23:21:10 -07:00
CONTRIBUTING.md Linking to the ReactJS code of conduct. (#26725) 2019-10-04 10:57:00 -07:00
ECOSYSTEM.md Add document describing the RN ecosystem. (#24493) 2019-04-17 09:06:37 -07:00
LICENSE Update copyright headers to yearless format 2018-09-11 15:33:07 -07:00
LICENSE-docs More licenses 2015-03-24 19:59:10 -07:00
README.md Update README.md (#26879) 2019-11-01 04:49:25 -07:00
React-Core.podspec Back out "Fixes Fabric related podspec" 2019-11-01 19:34:46 -07:00
React.podspec Move non-license comments out of license header 2019-10-15 20:12:12 -07:00
Releases.md Fix typo in Releases.md (#20487) 2018-08-01 07:16:56 -07:00
build.gradle.kts Tidy up license headers 2019-10-22 07:46:08 -07:00
cli.js Show full price instead of monthly price within the lead form 2019-03-28 16:07:19 -07:00
gradlew Update Gradle wrapper to 5.6.2 (#26349) 2019-09-06 00:25:53 -07:00
gradlew.bat Update Gradle wrapper to 5.6 (#26079) 2019-08-26 12:13:37 -07:00
index.js Remove unused code 2019-10-03 14:33:59 -07:00
interface.js Move `react-native-implementation.js` to `index.js` 2019-09-12 03:29:51 -07:00
jest-preset.js Delete hasteImpl, providesModuleNodeModules, and modulePathNameMapper (#24811) 2019-06-05 10:55:08 -07:00
jest.config.js Tidy up license headers [3/n] 2019-10-16 10:06:34 -07:00
metro.config.js Tidy up license headers [2/n] 2019-10-16 10:06:34 -07:00
package.json Upgrade metro version to 0.56.3 2019-10-31 10:08:46 -07:00
react-native.config.js Tidy up license headers [2/n] 2019-10-16 10:06:34 -07:00
react.gradle Fix error with bundling task eval. when no gradle config is provided (#27101) 2019-11-04 08:17:06 -08:00
rn-get-polyfills.js Remove Polyfills from RN Open Source 2019-04-04 15:20:33 -07:00
runXcodeTests.sh Move non-license comments out of license header 2019-10-15 20:12:12 -07:00
settings.gradle.kts Tidy up license headers 2019-10-22 07:46:08 -07:00
template.config.js Tidy up license headers [3/n] 2019-10-16 10:06:34 -07:00
yarn.lock Upgrade metro version to 0.56.3 2019-10-31 10:08:46 -07:00

README.md

React Native

Learn once, write anywhere:
Build mobile apps with React.

React Native is released under the MIT license. Current CircleCI build status. Current Appveyor build status. Current npm package version. PRs welcome! Follow @reactnative

Getting Started · Learn the Basics · Showcase · Contribute · Community · Support

React Native brings React's declarative UI framework to iOS and Android. With React Native, you use native UI controls and have full access to the native platform.

  • Declarative. React makes it painless to create interactive UIs. Declarative views make your code more predictable and easier to debug.
  • Component-Based. Build encapsulated components that manage their state, then compose them to make complex UIs.
  • Developer Velocity. See local changes in seconds. Changes to JavaScript code can be live reloaded without rebuilding the native app.
  • Portability. Reuse code across iOS, Android, and other platforms.

React Native is developed and supported by many companies and individual core contributors. Find out more in our ecosystem overview.

Contents

📋 Requirements

React Native apps may target iOS 9.0 and Android 4.1 (API 16) or newer. You may use Windows, macOS, or Linux as your development operating system, though building and running iOS apps is limited to macOS. Tools like Expo can be used to work around this.

🎉 Building your first React Native app

Follow the Getting Started guide. The recommended way to install React Native depends on your project. Here you can find short guides for the most common scenarios:

📖 Documentation

The full documentation for React Native can be found on our website.

The React Native documentation discusses components, APIs, and topics that are specific to React Native. For further documentation on the React API that is shared between React Native and React DOM, refer to the React documentation.

The source for the React Native documentation and website is hosted on a separate repo, @facebook/react-native-website.

🚀 Upgrading

Upgrading to new versions of React Native may give you access to more APIs, views, developer tools, and other goodies. See the Upgrading Guide for instructions.

React Native releases are discussed in the React Native Community, @react-native-community/react-native-releases.

👏 How to Contribute

The main purpose of this repository is to continue evolving React Native core. We want to make contributing to this project as easy and transparent as possible, and we are grateful to the community for contributing bug fixes and improvements. Read below to learn how you can take part in improving React Native.

Code of Conduct

Facebook has adopted a Code of Conduct that we expect project participants to adhere to. Please read the full text so that you can understand what actions will and will not be tolerated.

Contributing Guide

Read our Contributing Guide to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to React Native.

Open Source Roadmap

You can learn more about our vision for React Native in the Roadmap.

Good First Issues

We have a list of good first issues that contain bugs which have a relatively limited scope. This is a great place to get started, gain experience, and get familiar with our contribution process.

Discussions

Larger discussions and proposals are discussed in @react-native-community/discussions-and-proposals.

📄 License

React Native is MIT licensed, as found in the LICENSE file.

React Native documentation is Creative Commons licensed, as found in the LICENSE-docs file.