i have a little wondering question, right now im trying to learn react, and i have a login form, like this:
this.state = {
loginInputs: [
{
type: 'text',
id: 'loginForUser',
label: 'User',
key: 'user',
value: '',
error: ''
},
{
type: 'password',
id: 'loginForPassword',
label: 'Password',
key: 'pass',
value: '',
error: ''
}
]
}
and i map every input and render it in component.
also if the inputs are empty when you hit the login or register button, i want to update the array from state, and i wanted to create a usable function :
const isInvalidInput = (inputValue) => inputValue.length < 2;
updateError = (array) => {
const updatedArray = [...array];
updatedArray.forEach((input, index) => {
if(isInvalidInput(input.value)) {
updatedArray[index] = { ...input, error: 'errrrrrrorrrrrrrr'}
}
});
alert("yeee");
this.setState({
[array]: updatedArray
});
console.log(updatedArray);
}
and i invoke this function in loginhandler:
// login handler
handlerLogin = (e) => {
e.preventDefault();
const { loginInputs, accounts } = this.state;
if (inputname.length >= 2 && inputpass.length >=2) {
store in localhost...
} else {
this.updateError(loginInputs);
}
};
The problem is, when i let the fields empty and press the button, the function is called but the setState doesnt work, and errors from array are still empty. Can someone please help me with this?
thank you for your time :)