I want to store data in an existing array from a login form.
this is where i want to store my data.
const usersAccount = [
{
username: "user1",
password: "pass1"
}
];
and this is the function that get called on submit of the form
// check user when try to register
const registerUser = (event) => {
event.preventDefault();
var { uname, pass } = document.forms[0];
const username = uname.value;
const password = pass.value;
console.log(username, password);
};
username and password are the values i want to past in the userAccount
the form is an usual login form with 2 input fields and one button with type='submit'
CodePudding user response:
Are you not using any state management handlers for storing credentials?. If you want to store credentials in exisiting array.
const registerUser = (event) => {
event.preventDefault();
var { uname, pass } = document.forms[0];
const username = uname.value;
const password = pass.value;
usersAccount[0].username = username
usersAccount[0].password = password
};
But, using the above approach is a bad way of storing credential.
A good approach could be to store the existing array as in the useState
const [cred,setCred] = useState(usersAccount)
const registerUser = (event) => {
event.preventDefault();
var { uname, pass } = document.forms[0];
const username = uname.value;
const password = pass.value;
const newUserPass = {username, password}
setCred([...newUserPass])
};