Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Cambiar el texto de un Text en React Native

Tiempo de lectura: < 1 minuto

Si queremos cambiar el contenido de un Text utilizando React Native (hacer un setText) podemos utilizar el siguiente código que voy a explicar.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//imports necesarios, vamos a utilizar useState
import React, { useState } from "react";
import {Text, Button} from "react-native";
//Crear la vista
const App = () => {
//Estados para actualizar textos:
const [texto, setText] = useState("Hola Mundo");
//Funcion del boton
function cambiarTexto(){
setText("Hola React");
}
return (
//Texto
<Text>{textoInicio}</Text>
//Boton
<Button onPress={cambiarTexto} >Cambiar Texto</Button>
)
};
//imports necesarios, vamos a utilizar useState import React, { useState } from "react"; import {Text, Button} from "react-native"; //Crear la vista const App = () => { //Estados para actualizar textos: const [texto, setText] = useState("Hola Mundo"); //Funcion del boton function cambiarTexto(){ setText("Hola React"); } return ( //Texto <Text>{textoInicio}</Text> //Boton <Button onPress={cambiarTexto} >Cambiar Texto</Button> ) };
//imports necesarios, vamos a utilizar useState
import React, { useState } from "react";
import {Text, Button} from "react-native";

//Crear la vista
const App = () => {
    //Estados para actualizar textos:
    const [texto, setText] = useState("Hola Mundo");
    
    //Funcion del boton
    function cambiarTexto(){
      setText("Hola React");
    }
 
    return (
            //Texto 
            <Text>{textoInicio}</Text>
            //Boton
            <Button onPress={cambiarTexto} >Cambiar Texto</Button>
           )
};


Ahora voy a explicar el código.

Primero se crea una constante de estados, primero indica el parámetro con el texto (texto) y el segundo atributo (setText) es la función para cambiar el texto. El useState, contiene el valor por defecto en este caso «Hola Mundo».

const [texto, setText] = useState("Hola Mundo");
const [texto, setText] = useState("Hola Mundo");

Luego he creado una función para utilizar con el botón:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function cambiarTexto(){
setText("Hola React");
}
function cambiarTexto(){ setText("Hola React"); }
function cambiarTexto(){
  setText("Hola React");
}

Aquí dentro, se llama a la función que hemos creado anteriormente llamada setText.

Ahora se asigna el texto al componente text de React Native.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Text>{textoInicio}</Text>
<Text>{textoInicio}</Text>
<Text>{textoInicio}</Text>

Al pasar textoInicio, se le pasa la variable y cambiará cuando se llame a setText.

Ahora se crea un botón y se asigna la acción de cambiar el texto con el método onPress.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<Button onPress={cambiarTexto} >Cambiar Texto</Button>
<Button onPress={cambiarTexto} >Cambiar Texto</Button>
        <Button onPress={cambiarTexto} >Cambiar Texto</Button>
2

Deja un comentario