-1
I have a problem with a React application. Whenever I activate the onBlur={}
, error occurs TypeError: Cannot read property 'valido' of undefined'
. How can I solve?
Code of Signup.jsx clients
import React, { useState } from "react";
import "./CadastroClientes.css";
import {
Button,
TextField
} from "@material-ui/core";
function CadastroClientes({ enviarCliente, validarNomeEmpresa, validarNomePonto }) {
const data = ['Ativo', 'Inativo']
const [nomeEmpresa, setNomeEmpresa] = useState("");
const [nomePonto, setNomePonto] = useState("");
const [codigo, setCodigo] = useState("");
const [descricaoSICP, setDescricaoSICP] = useState("");
const [status, setStatus] = useState("Ativo");
const [pais, setPais] = useState("");
const [estado, setEstado] = useState("");
const [cidade, setCidade] = useState("");
const [bairro, setBairro] = useState("");
const [endereco, setEndereco] = useState("");
const [lat, setLat] = useState("");
const [long, setLong] = useState("");
const [erros, setErros] = useState({nomeEmpresa: {valido: true, texto: ""}, nomePonto: {valido: false, texto: ""}, codigo: {valido: true, texto: ""}, descricaoSICP: {valido: true, texto: ""}, estado: {valido: true, texto: ""}, cidade: {valido: true, texto: ""}, bairro: {valido: true, texto: ""}, endereco: {valido: true, texto: ""}, lat: {valido: true, texto: ""}, long: {valido: true, texto: ""}});
return (
<div className="container">
<form
onSubmit={(event) => {
enviarCliente({nomeEmpresa, nomePonto, codigo, descricaoSICP, status, pais, estado, cidade, bairro, endereco, lat, long});
event.preventDefault();
}}
>
<h1>Cadastro de Clientes</h1>
<TextField
value={nomeEmpresa}
onBlur={(event) => {
const ehValido = validarNomeEmpresa(nomeEmpresa);
setErros({nomeEmpresa: ehValido});
}}
onChange={(event) => {
setNomeEmpresa(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="nomeEmpresa"
error={!erros.nomeEmpresa.valido}
helperText={erros.nomeEmpresa.texto}
label='Nome Empresa'
fullWidth
/>
<div className="flex">
<TextField
value={nomePonto}
onBlur={(event) => {
const isValid = validarNomePonto(nomePonto);
setErros({nomePonto: isValid});
}}
onChange={(event) => {
setNomePonto(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="nomePonto"
error={!erros.nomePonto.valido}
helperText={erros.nomePonto.texto}
label="Nome do Ponto"
className="w50"
/>
<TextField
value={codigo}
onChange={(event) => {
setCodigo(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="codigo"
label="Código"
className="w50"
/>
</div>
<TextField
value={descricaoSICP}
onChange={(event) => {
setDescricaoSICP(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="descricaoSICP"
label="Descrição SICP"
fullWidth
/>
<label for="exampleFormControlSelect1">Status</label>
<select value={status} onChange={(event) => {
setStatus(event.target.value)
}} class="form-control" id="exampleFormControlSelect1">
{
data.map(i => (
<option>{i}</option>
))
}
</select>
{/* Localização */}
<h2>Localização</h2>
<TextField
value={pais}
onChange={(event) => {
setPais(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="pais"
label="País"
fullWidth
/>
<div className="flex">
<TextField
value={estado}
onChange={(event) => {
setEstado(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="estado"
label="Estado"
className="w50"
/>
<TextField
value={cidade}
onChange={(event) => {
setCidade(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="cidade"
label="Cidade"
className="w50"
/>
</div>
<TextField
value={bairro}
onChange={(event) => {
setBairro(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="bairro"
label="Bairro"
fullWidth
/>
<TextField
value={endereco}
onChange={(event) => {
setEndereco(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="endereco"
label="Endereço"
fullWidth
/>
<div className="flex">
<TextField
value={lat}
onChange={(event) => {
setLat(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="lat"
label="Latitude"
className="w50"
/>
<TextField
value={long}
onChange={(event) => {
setLong(event.target.value);
}}
color="primary"
variant="outlined"
margin="normal"
id="long"
label="Longitude"
className="w50"
/>
</div>
<Button type="submit" id="buttonCadastro" variant="contained" color="primary">Cadastrar Cliente</Button>
</form>
</div>
);
}
export default CadastroClientes;
strange response, because
useState
also has how to merge! or not as you said? only if I got it wrong, andsetState
andthis.seState
are not the same thing? I can do the same thing in both ways bothComponent
asfunction
Component
... ! I really didn’t understand your answer.– novic
@novic edited the answer to make more clear what he meant. The
useState
andsetState
have different behaviors as explained in the answer, in the documentation and as can be verified in this example which I have just created. It is possible to merge yes, as I did with the Operator spread, but it’s not standard behavior.– Rafael Tavares
Thanks for the reply! But I still don’t understand what has to be changed in my code.
– Jaburu
@Jaburu Seus
setErros
within theonBlur
... They need to merge the old state with the new state (as in the answer code) rather than just replacing the state (as in their code). Emphasis on...erros
– Rafael Tavares
I did it and it didn’t hurt ☹
– Jaburu
@Jaburu you were modifying the structure of the object
nomeEmpresa
, in addition to the previously mentioned status override error, however I had not noticed it. I fixed the code now, take a look... It is necessary to maintain the same structure as the initial one (withtexto
andvalido
) instead of replacing this object with a Boolean, like. is in your code (nomeEmpresa: ehValido
). Pay attention to others tooonBlur
to respect the correct structure defined inuseState
– Rafael Tavares