Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

160
Vistas
How to pass this.state to a component on the same page?

Hey Im trying to pass through a state from a constructor component to a function component both on the same page. Just want to sync the first state with the second component. Basically I'm getting a text from the first and want to pass it to the second, anyone know how to do it ? Thanks

class Input extends Component{
  constructor(props){
    super(props);
    this.state = {
      fileName: '',
      fileContent: ''
    };
  }

  handleFile = e => {
    const file = e.target.files[0];
    const reader = new FileReader();
    reader.readAsText(file);
    reader.onload = () => {
      this.setState({fileName: file.name, fileContent: reader.result});
      if(reader.result.includes('//')){
        let prevPart = null;

        var newText1 = reader.result
        newText1 = newText1
                .split('\n')
                .filter(x => !x.includes('\/\/'))
                .join('\n')
        
        const newText2 = newText1.split('/').map(part => {
            if (part.startsWith('*') && part.endsWith('*')) {
                  prevPart = null;
                  return null;
                }
              
            if (prevPart !== null) {
                  part = '/' + part;
                }
              
                prevPart = part;
              
                return part;
              }).filter(part => part !== null).join('');
    
        this.setState({fileName: file.name, fileContent: newText2})
      
      }
    }
    reader.onerror = () =>{
      console.log('File Error : ', reader.error)
    }
  }

  render(){
    return(
      <>
      <br/><br/><br/>
      <div className='upload_file'>
      <br/><br/>
      <h1>Choose File to import</h1>
      <br/><br/>
      <label class="custom-file-upload">
      <input type='file' onChange={this.handleFile}></input>Choose File</label><br/><br/><br/>
      <button className='btn_disp'><Link to='/output'>
        Show File
      </Link></button>
      <br/><br/>
      <p>File Name : {this.state.fileName}</p>
      <p>{this.state.fileContent}</p>
      </div>
      </>
    )
  }
}

const Display = (props) =>{
  return(
    <>
    <h2>
      Display File
    </h2>
    <p>{props.fileContent}</p>
    </>
  );
};

I want the this.state.fileContent to pass through the Display component that are next to each other

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda