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

158
Visualizações
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 à 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