Skip to content
Advertisement

Easy problem : Trying to change content when button is press React native

I’m actualy coding a mobile app for school and I have problems when try to change content of a Text when I’m pressing a TouchableOpacity button.

I want that : When I press the button (actualy a text button) it change the content of a Text by the actual text of the button (imagine menu where you have to chose an app ex: discord/yammer etc… and when you press on the app you want it change the actual ‘…’ by the name of the app)

visual to help you :enter image description here

To do this I wrote this :

JavaScript

I create a let variable named ‘content’ = ‘…’ and I set the action of the button to replace the content by the title of the ‘option’

like this :

JavaScript

but it didn’t work, that didn’t change the content when I press the button (Spotify or something).

Thanks a lot in advance guys, love u

Advertisement

Answer

The view only re-renders when a state changes, content is a let. You could solve the problem by changing the content to a state. Change the content to the option title when onPress is fired.

JavaScript

Another option, if you really want to handle it from the action in the array, could be to pass the option into the function

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement