如何在React中获取多个用户

huangapple go评论81阅读模式
英文:

How can get multiple users in React

问题

I want to get some user in localstorage and use it in login page.
In the following code, I was able to save only one user in localstorage, and by adding a new user, the old user is deleted and the new user is replaced.
But I want the old user not to be deleted by adding each new user and I want to add as many users as possible.

sign in page:

const Signin = () => {

    const [values, setValue] = useState({ email: "", password: "" });
    const navigate = useNavigate();

    const onChangeInput = (name,e) => {

        setValue(pvalue => ({
            ...pvalue,
            [name]: e.target.value,
        }));

    };

    const submit = () => {
        let users = JSON.parse(localStorage.getItem("users") || "[]")
        users.push(values)

        localStorage.setItem('users', JSON.stringify(users))
        
    }

    function handleSubmit(e) {
        e.preventDefault()
        localStorage.setItem("user", JSON.stringify(values));

        const loggeduser = JSON.parse(localStorage.getItem("user"));
        if (
            values.email === loggeduser.email &&
            values.password === loggeduser.password
        ) {
            alert("Email & Password is available!!!")
        }
        else{
            navigate("/");
        }
    };

    return (
        <div className="text-center m-5-auto">
            <form onSubmit={submit}>
                <p>
                    <label>Email address:</label><br />
                    <input
                        name="email"
                        value={values.email}
                        onChange={(e)=>onChangeInput('email',e)}
                    />
                </p>
                <p>
                    <label>Password:</label><br />
                    <input
                        name="password"
                        value={values.password}
                        onChange={(e)=>onChangeInput('password',e)}
                    />
                </p>
                <p>
                    <button id="btn" type="submit" onClick={handleSubmit}>Register</button>
                </p>
            </form>
        </div>
    );
}

export default Signin;

I did various searches and did not find any results

英文:

I want to get some user in localstorage and use it in login page.
In the following code, I was able to save only one user in localstorage, and by adding a new user, the old user is deleted and the new user is replaced.
But I want the old user not to be deleted by adding each new user and I want to add as many users as possible.

sign in page:

const Signin = () =&gt; {
const [values, setValue] = useState({ email: &quot;&quot;, password: &quot;&quot; });
const navigate = useNavigate();
const onChangeInput = (name,e) =&gt; {
setValue(pvalue =&gt; ({
...pvalue,
[name]: e.target.value,
}));
};
const submit = () =&gt; {
let users = JSON.parse(localStorage.getItem(&quot;users&quot;) || &quot;[]&quot;)
users.push({values})
localStorage.setItem(&#39;users&#39;, JSON.stringify(users))
}
function handleSubmit(e) {
e.preventDefault()
localStorage.setItem(&quot;user&quot;, JSON.stringify(values));
const loggeduser = JSON.parse(localStorage.getItem(&quot;user&quot;));
if (
values.email === loggeduser.email &amp;&amp;
values.password === loggeduser.password
) {
alert(&quot;Emaill &amp; Password is available!!!&quot;)
}
else{
navigate(&quot;/&quot;);
}
};
return (
&lt;div className=&quot;text-center m-5-auto&quot;&gt;
&lt;form onSubmit={submit}&gt;
&lt;p&gt;
&lt;label&gt;Email address:&lt;/label&gt;&lt;br /&gt;
&lt;input
name=&quot;email&quot;
value={values.email}
onChange={(e)=&gt;onChangeInput(&#39;email&#39;,e)}
/&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;label&gt;Password:&lt;/label&gt;&lt;br /&gt;
&lt;input
name=&quot;password&quot;
value={values.password}
onChange={(e)=&gt;onChangeInput(&#39;password&#39;,e)}
/&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;button id=&quot;btn&quot; type=&quot;submit&quot; onClick={handleSubmit}&gt;Register&lt;/button&gt;
&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
);
}
export default Signin;

I did various searches and did not find any results

答案1

得分: 0

我希望以下的解决方案可能会对你有所帮助:

function handleSubmit(e) {
    e.preventDefault();
    if (
        values.email === loggeduser.email &&
        values.password === loggeduser.password
    ) {
        alert("邮箱和密码可用!");
    
        let users = JSON.parse(localStorage.getItem("users") || "[]");
        users.push({values});

        localStorage.setItem('users', JSON.stringify(users));
    }
    else {
        navigate("/");
    }
};
英文:

I hope following solution might help you :

 function handleSubmit(e) {
e.preventDefault()
if (
values.email === loggeduser.email &amp;&amp;
values.password === loggeduser.password
) {
alert(&quot;Emaill &amp; Password is available!!!&quot;)
let users = JSON.parse(localStorage.getItem(&quot;users&quot;) || &quot;[]&quot;)
users.push({values})
localStorage.setItem(&#39;users&#39;, JSON.stringify(users))
}
else{
navigate(&quot;/&quot;);
}
};

huangapple
  • 本文由 发表于 2023年3月12日 14:49:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711493.html