VideoDetailScreen that can be focused, we will create our own button component. This also ensures that we are not duplicating code and the button can be re-used across our App.
Define Button component
-
Create a new file under the components folder called
Button.tsx. - Import React and useState from ‘react’.
- Import StyleSheet, Text and TouchableOpacity from ‘react-native’.
-
Create an arrow function called Button that takes in 2 arguments called
pressFunctionandbuttonText. -
Destructure each argument in the interface and add
:IPropsto enforce the type of each argument in the object. ThepressFunctionwill be a Function and thebuttonTexta string. -
Below the import statements, create an interface that declares the types of the props.
- Add an empty fragment and inside it add a TouchableOpacity element.
- Add the onPress prop to TouchableOpacity and set its value to call the pressFunction
- Add a Text component inside the TouchableOpacity and set it to take in the value of buttonText.
onFocus() and onBlur() properties to the TouchableOpacity
- Import
useStatefrom react and initialize it with a state offocusedand a function ofsetFocused. - Initialize the value of
focusedto false.
- Add an
onFocusprop to the TouchableOpacity, and set it to call thesetFocused()function with the valuetrue - Add an
onBlurprop to the TouchableOpacity, and set it to call thesetFocused()function with the valuefalse
Add Styling
Now let’s add some styling:- Create a StyleSheet called styles and add it after the Button function.
- Add
buttonContainer,buttonTextandfocusedstyles to the StyleSheet with the following values:
- Give the touchable opacity a style prop of buttonInnerContainer and a conditional style prop of focused
- Add the buttonOuterContainer style to the View component.

