Skip to content

Commit

Permalink
Merge pull request #27808 from Nikhil-Vats/update_docs
Browse files Browse the repository at this point in the history
Update docs to add guidelines to auto-focus TextInput
  • Loading branch information
roryabraham authored Sep 20, 2023
2 parents f821a37 + 39d5603 commit 9533031
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions contributingGuides/STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,28 @@ A `useEffect()` that does not include referenced props or state in its dependenc

There are pros and cons of each, but ultimately we have standardized on using the `function` keyword to align things more with modern React conventions. There are also some minor cognitive overhead benefits in that you don't need to think about adding and removing brackets when encountering an implicit return. The `function` syntax also has the benefit of being able to be hoisted where arrow functions do not.
## How do I auto-focus a TextInput using `useFocusEffect()`?
```javascript
const focusTimeoutRef = useRef(null);
useFocusEffect(useCallback(() => {
focusTimeoutRef.current = setTimeout(() => textInputRef.current.focus(), CONST.ANIMATED_TRANSITION);
return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, []));
```
This works better than using `onTransitionEnd` because -
1. `onTransitionEnd` is only fired for the top card in the stack, and therefore does not fire on the new top card when popping a card off the stack. For example - pressing the back button to go from the workspace invite page to the workspace members list.
2. Using `InteractionsManager.runAfterInteractions` with `useFocusEffect` will interrupt an in-progress transition animation.
Note - This is a solution from [this PR](https://github.com/Expensify/App/pull/26415). You can find detailed discussion in comments.
# Onyx Best Practices
[Onyx Documentation](https://github.com/expensify/react-native-onyx)
Expand Down

0 comments on commit 9533031

Please sign in to comment.