-1
I need to recover an input value from my login screen and move to my home screen.
I made a document.querySelector
to access my input through ID
and stored this value in a const chamanda empresa
.
Inside my home I imported this constant and made a console.log to check if I was receiving the data, at the same time I inserted it inside my setState to get the position of the Array equal to the company number. But the value returns Undefined.
I also get a warning:
Warning: Failed prop type: You provided a
value
prop to a form field without anonChange
Handler. This will render a read-only field. If the field should be mutable usedefaultValue
. Otherwise, either setonChange
orreadOnly
.
How can I fix this?
Login screen:
import React, { Component } from 'react'
import {LoginService} from "../../services/LoginService"
import './loginPage.css'
//Recebe o número da empresa
export const empresa = parseInt(document.querySelector("#empresa").value)
class Login extends Component {
fazerLogin = infosDoEvento => {
infosDoEvento.preventDefault();
const dadosDeLogin = {
login: this.refs.inputLogin.value,
senha: this.refs.inputSenha.value,
};
console.log(dadosDeLogin)
LoginService.logar(dadosDeLogin)
.then(() => {
this.props.history.push("/home");
})
.catch(err => {
console.error(`[Erro ${err.status}]`, err.message);
});
}
render() {
return (
<div className="loginPage">
<div className="container">
<h2 className="loginPage__title">Seja bem vindo!</h2>
<form className="loginPage__form" action="/" onSubmit={this.fazerLogin}>
<div className="loginPage__inputWrap">
<label className="loginPage__label" htmlFor="login">Login</label>
<input ref="inputLogin" className="loginPage__input" type="text" id="login" name="senha"/>
</div>
<div className="loginPage__inputWrap">
<label className="loginPage__label" htmlFor="senha">Senha</label>
<input ref="inputSenha" className="loginPage__input" type="password" id="senha" name="senha"/>
</div>
<div>
<label className="loginPage__label" htmlFor="login">Empresa</label>
<input ref="inputEmpresa" className="loginPage__input" type="text" id="empresa" name="empresa" />
</div>
<br></br>
<div className="loginPage__inputWrap">
<button className="loginPage__btnLogin" type="submit">
Logar
</button>
</div>
</form>
</div>
</div>
)
}
}
export default Login;
Home screen:
import React, { Component } from 'react'
import { Container, Table} from 'react-bootstrap'
import Extrato from '../Extrato'
import NavBar from '../NavBar'
import {consultarResumo} from '../../services/Home/index'
//Importa o valor da Empresa
import {empresa} from '../Login/index'
//Imprime o valor da Empresa
window.onload = teste => {console.log(empresa)}
class Home extends Component {
constructor(props){
super(props);
this.state = {
empresaId: 0,
nomeEmpresa: "EMPRESA UM S/A",
cnpj: 0,
dadosBancario: {
banco: 0,
bancoNome: "CONTA SIMPLES",
agencia: 0,
conta: 0,
digitoConta: 0
},
saldo: 0
}
}
componentDidMount(){
//Consulta na API e busca a posição igual o valor da empresa
consultarResumo().then(dados => this.setState(dados[empresa]))
}
render() {
return (
<Container>
<NavBar></NavBar>
<br/>
<Table striped bordered hover variant="dark">
<thead>
<tr>
<th>{this.state.nomeEmpresa}</th>
<th>CNPJ: {this.state.cnpj}</th>
<th>Número do Banco: {this.state.dadosBancario.banco}</th>
<th>{this.state.dadosBancario.bancoNome}</th>
<th>Agência: {this.state.dadosBancario.agencia}</th>
<th>Conta: {this.state.dadosBancario.conta}</th>
<th>Digito da Conta: {this.state.dadosBancario.digitoConta}</th>
<th>Saldo: {this.state.saldo.toLocaleString("PT-BR", {style: "currency", currency : "BRL"})}</th>
</tr>
</thead>
</Table>
<Extrato></Extrato>
</Container>
)
}
}
export default Home;
All right here
parseInt(document.querySelector("#empresa")).value
? would not beparseInt(document.querySelector("#empresa").value)
?– Cmte Cardeal
Even changing position, the problem persists.
– user210629
Yes, it was just a detail that I called attention to solve. Now change
onSubmit={this.fazerLogin}
foronSubmit={(event) => this.fazerLogin(event)}
and see if the Warning still appears– Cmte Cardeal
Warning continues, but now we have another error: Cannot read Property 'value' of null at Module.. /src/View/Login/index.js and the variable appears as Undefined in the console on the home screen.
– user210629
Okay, I’ll write my answer with the tests I did here and we’ll see if it solves your problem
– Cmte Cardeal