Skip to content
the poor coder

react native button

Poor Man
Published
Reading time
2 min read
On this page

React Native Button#

React Native Button is a component that allows users to interact with the application by pressing it. Buttons can be customized with different colors, shapes, and sizes to fit the design of the application. It is one of the most commonly used components in React Native.

Creating a Button Component#

To create a button component in React Native, we can use the TouchableHighlight or TouchableOpacity component provided by React Native.

TouchableHighlight: This component provides a simple feedback mechanism when the button is pressed. When the button is pressed, the background color of the button changes, and when it is released, it goes back to its original color.

JavaScript

<TouchableHighlight
  style={{ backgroundColor: "blue" }}
  onPress={() => console.log("Button Pressed")}
>
  <Text style={{ color: "white" }}>Press Me</Text>
</TouchableHighlight>

TouchableOpacity: This component provides a more subtle feedback mechanism when the button is pressed. When the button is pressed, the opacity of the button decreases slightly, and when it is released, it goes back to its original opacity.

JavaScript

<TouchableOpacity
  style={{ backgroundColor: "blue" }}
  onPress={() => console.log("Button Pressed")}
>
  <Text style={{ color: "white" }}>Press Me</Text>
</TouchableOpacity>

Passing Props to Button Component#

The button component can be customized by passing props to it.

  • onPress: This prop is used to define the function to be executed when the button is pressed.
  • style: This prop is used to define the style of the button.
  • disabled: This prop is used to disable the button.
  • title: This prop is used to define the text displayed on the button.
  • accessibilityLabel: This prop is used to help users with disabilities to understand what the button does.
JavaScript

<TouchableOpacity
  style={{ backgroundColor: "blue" }}
  onPress={() => console.log("Button Pressed")}
  disabled={false}
  title="Press Me"
  accessibilityLabel="Press Me Button"
>
  <Text style={{ color: "white" }}>Press Me</Text>
</TouchableOpacity>

Conclusion#

React Native Button is an essential component that is used in almost every application. By using the above examples, we can create customized buttons that match the design of our application. We can also pass props to the button component to define its behavior and accessibility.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC