Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

251
Views
Cómo obtener el estado de los niños de los padres en el componente de clase

Estoy tratando de mostrar el estado del componente secundario en el componente principal, pero de alguna manera el texto "abcde" aún no se muestra, aquí está el código de muestra

 import React from 'react'; import {SafeAreaView, StyleSheet, Text} from 'react-native'; import {TouchableOpacity} from 'react-native-ui-lib'; class ChidComponent extends React.Component { constructor(props) { super(props); this.state = { text: '', }; } render() { return ( <SafeAreaView> <TouchableOpacity onPress={() => { this.setState({text: 'abcde'}); this.props.getText(this.state.text); }}> <Text>Get Text</Text> </TouchableOpacity> </SafeAreaView> ); } } const style = StyleSheet.create({ text: { fontWeight: 'bold', fontSize: 40, }, }); class ParentComponent extends React.Component { constructor(props) { super(props); this.state = { text: '', }; } getTextFromChild(value) { this.setState({text: value}); } render() { return ( <SafeAreaView> <Text>{this.state.text}</Text> <ChidComponent getText={this.getTextFromChild.bind(this)} press={() => this.setState({show: !this.state.show})} textStyle={style.text} /> </SafeAreaView> ); } } export default ParentComponent;

Pero de alguna manera la pantalla todavía está vacía, el 'abcde' aún hasta que hago clic dos veces , proviene de un componente funcional, así que no sé qué está pasando, por favor ayuda, muchas gracias.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Su método getTextFromChild en ParentComponent está eliminando la propiedad show del estado, actualícelo a esto:

 getTextFromChild(value) { this.setState({text: value, show: true }); }

EDITAR: setState es asíncrono, puede pasar una función de devolución de llamada como argumento para acceder al estado actualizado.

 <TouchableOpacity onPress={() => { this.setState({text: 'abcde'}, () => { this.props.getText(this.state.text); }); }}>

Deberia trabajar.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!