Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Cómo pasar datos de un componente a otro en reaccionar nativo

tengo un componente principal donde he importado un componente, así que quiero pasar algunos datos de ese componente importado al componente principal y mostrarlo. aquí hay un código de ejemplo: este es mi archivo principal:

 import React from "react"; import { View, Text, StyleSheet, TouchableOpacity, Image } from "react-native"; import TestComponent from "../components/TestComponent"; function TestScreen(props) { return ( <View> <TestComponent /> <Text>{location}</Text> </View> ); } export default TestScreen;

y este es el componente importado:

 import React from "react"; import { View, Text, StyleSheet, TouchableOpacity, Image } from "react-native"; function TestComponent(props) { const [location, setLocation] = useState(""); return ( <View> <Text>Boiler</Text> <TouchableOpacity> <Text onPress={() => setLocation("Me")}>Click</Text> </TouchableOpacity> </View> ); } export default TestComponent;

ahora quiero obtener el enlace de location del componente TestComponent y usarlo en el componente TestScreen , ¿cómo puedo hacer eso?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Debe elevar el estado al componente principal

 function TestScreen(props) { const [location, setLocation] = useState(""); return ( <View> <TestComponent setLocation={setLocation} location={location}/> <Text>{location}</Text> </View> ); } function TestComponent({ location, setLocation }) { return ( <View> <Text>Boiler</Text> <TouchableOpacity> <Text onPress={() => setLocation("Me")}>Click</Text> </TouchableOpacity> </View> ); }

Y ahora tienes acceso a location en el padre

about 4 years ago · Juan Pablo Isaza Relatório

0

Ha declarado sus datos y pasado los datos como apoyo al componente secundario, los datos principales pueden ser una matriz, objetos, estados, etc.

 import React from "react"; import { View, Text, StyleSheet, TouchableOpacity, Image } from "react-native"; import TestComponent from "../components/TestComponent"; function TestScreen(props) { const data = {name: 'im parent data'; otherProperty: 'im other property'} return ( <View> <TestComponent /> PASS DATA AS PROP <Text data={data}>{location}</Text> </View> ); } export default TestScreen;

Acceso a los datos principales con notación de puntos:

 import React from "react"; import { View, Text, StyleSheet, TouchableOpacity, Image } from "react-native"; function TestComponent(props) { const [location, setLocation] = useState(""); return ( <View> <Text>Boiler</Text> <TouchableOpacity> <Text onPress={() => setLocation("Me")}>Click</Text> <h1>{props.data.name}</h1> <h1>{props.data.otherProperty}</h1> </TouchableOpacity> </View> ); } export default TestComponent;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda