2
I’m developing an application with React with Hooks and functional components, and I came across the following logic: I need to screen render the functional components, both with Forms arrays inside the main component, instead of sending the information to my API within each component I found it more practical for the child components to pass the arrays to the parent component and send it once, so I thought I’d pass as a props a function that arrow the array into a state of the parent component, but every time I try to access this function from the child component of an error.
Parent component:
function Config(){
const [registerData, setRegisterData] = useState([])
const [deleteData, setDeleteData] = useState(null)
function setRegister(dados){
setRegisterData(dados)
}
function setDelete(dados){
setDeleteData(dados)
}
console.log(registerData, deleteData)
return(
<div>
<Register data={setRegister} />
<br />
<Delete data={setDelete} />
</div>
)
}
export default Config;
Child component:
function Register({props}){
const [usuario, setUsuario] = useState('')
const [telefone, setTelefone] = useState('')
const [unidade, setUnidade] = useState('')
const [departamento, setDepartamento] = useState('')
const [cargo, setCargo] = useState('')
const form = {usuario: usuario, telefone: telefone, unidade: unidade, departamento: departamento, cargo: cargo}
props.setRegister(form);
return(
<div className="app">
<center>
<input type="text" onChange={(e) => setUsuario(e.target.value)} className="form-input" placeholder="Usuário" />
<br />
<br />
<input type="text" onChange={(e) => setTelefone(e.target.value)} className="form-input" placeholder="Telefone" />
<br />
<br />
<select>
<option onChange={(e) => setUnidade(e.target.value)}>Selecione uma unidade</option>
<option value="UB">UB</option>
<option value="UG">UG</option>
<option value="UM">UM</option>
<option value="UP">UP</option>
<option value="UV">UV</option>
<option value="UL">UL</option>
</select>
<br />
<br />
<input type="text" onChange={(e) => setDepartamento(e.target.value)} className="form-input" placeholder="Departamento" />
<br />
<br />
<input type="text" onChange={(e) => setCargo(e.target.value)} className="form-input" placeholder="Cargo" />
<br /><br />
<br /><br />
<button className="btn btn-lg btn-dark">Cadastrar</button>
</center>
</div>
)
}
export default Register;
The mistake that happens:
TypeError: Cannot read property 'setRegister' of undefined
When I hit my eye, I see two errors in your code (I don’t know if you have more). 1)
Register({props})
will receive a props with the nameprops
(that is to say,props.props
); 2)setRegister
is not the name of the props, but yesdata
(which is a bad name, tidy this up to make it easier to read and understand what it means)– Rafael Tavares
The problem here has nothing to do with React. It is the functioning of the Javascript breakdown.
– Luiz Felipe
Actually, this edition of props I did to conduct a test. So assuming my component is "Register({setRegister})" and in the parent component I pass this way : <Register setRegister={setRegister} />, as I would access this function within the child?
– Geovane
How so @Luizfelipe?
– Geovane