I'm using TouchableOpacity
to make a button as I'm learning react-native.
But the problem is, TouchableOpacity
takes 100% width of the screen. But, I want to take the size/grow with the component present inside it.
How can I do it?
import React, { Component } from "react";
import { Text, TouchableOpacity } from "react-native";
export default class App extends Component {
render(){
return(
<TouchableOpacity
onPress={() => console.log("Pressed!")}
style={{ backgroundColor: "red" }}
>
<Text>Press me!</Text>
</TouchableOpacity>
);
}
When I decrease the width of the TouchableOpacity
like 10 or 20, it automatically increases its height to fit the Text
.
So, it possible for TouchableOpacity
to grow with the size of component present inside it?
This component fades out when pressed, and fades back in when released. We can style it however we want, just like a View . We can configure the pressed opacity with the activeOpacity prop.
How do I change the size of the button text in React Native? Unfortunately, according to the documentation (https://reactnative.dev/docs/button) you can't change a font-size of a button. The only style prop you can change is color .
On press down, the opacity of the wrapped view is decreased, dimming it. Opacity is controlled by wrapping the children in an Animated.View , which is added to the view hierarchy. Be aware that this can affect layout.
You can use the View#hitSlop property to increase the touchable area. This can be useful in scenarios where you know that the increased touch area won't overlap with other touchables. A more robust solution is to use the padding style property.
You can actually use alignSelf: 'flex-start' or 'flex-end' depending on which side you want to pin
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With