简体   繁体   English

如何将 object state 的所有键设置为空数组并在 React js 中仅更改其中一个

[英]How can I set all the key of an object state to empty array and change just one of them in React js

I'm creating sign up form with next.js and I'm handling errors with joi.js, After Fetching the data if there is error, I want if the error is in the name input I will pass it as value of the name key in the errors state and pass an empty string to the other keys (email and password), and same thing for other errors, I tried to do it but when there is no error, the keys in the errors state have always the past values.我正在使用 next.js 创建注册表单,我正在使用 joi.js 处理错误,如果有错误,在获取数据后,我想如果错误出现在名称输入中,我将把它作为名称键的值传递给错误 state 并将空字符串传递给其他键(电子邮件和密码),对于其他错误也是如此,我尝试这样做但是当没有错误时,错误 state 中的键始终具有过去的值。

(Sorry for my bad English) (对不起,我的英语不好)

我的注册表单和控制台

File where I fetch data Login.tsx我从中获取数据的文件Login.tsx

 export default function Login() { const [data, setData] = useState({ name: "", email: "", password: "" }); const [errors, setErrors] = useState({ name: "", email: "", password: "", }); const handleChange = (e) => { setData({...data, [e.target.name]: e.target.value, }); }; const handleSubmit = async (e) => { e.preventDefault(); const res = await fetch("/api/users", { body: JSON.stringify(data), headers: { "Content-Type": "application/json", }, method: "POST", }, { cache: 'no-store' }); const content = await res.json(); if(content.error) { setErrors({...errors, [content.error[0].path[0]]:content.error[0].message}) } console.log(errors); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

There will always be the old values in the errors state variable since you're setting the new error by spreading the old ones first: setErrors({...errors, *****}) errors state 变量中总会有旧值,因为您通过首先传播旧值来设置新错误: setErrors({...errors, *****})

What does your content.error contain?你的content.error包含什么? I can see you're only accessing the first element ( content.error[0] ), but do you receive all errors for that submission?我可以看到您只访问了第一个元素 ( content.error[0] ),但是您是否收到了该提交的所有错误? If you do, you don't need to spread the old errors, just run through all of the reported errors from the response and recreate your errors object.如果这样做,则不需要传播旧错误,只需运行响应中报告的所有错误并重新创建errors object。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React JS 中使用 useState 在 state object 中将 object 的数组设置为空? - How to set array of object empty in state object using useState in React JS? 如何在React中的状态下更改数组? - How can I change an array in a state in React? 如何在 react/JS 中映射数组和 object 后设置 State? - How to set State after mapping over an array and object in react/JS? 如何在 React state 的对象数组中更改一个 object 的属性? - How to change property of one object in an array of objects in React state? React - 如何访问不同 .js 文件中的一个 .js 文件中的状态/数组? - React - How can I access a state/array that's in one .js file in a different .js file? 我如何通过链接传递其状态在一个组件中设置的对象的属性,并在 React 中的另一个组件中呈现它们 - How do i pass the properties of an object whose state is set in one component through a link and render them in another component in React 如何从多个 arrays 中获取每个 object 并将它们全部放入一个数组中 - How can I get each object from multiple arrays and put them all into one array 我不能在状态中设置一个空数组 - I can't set an empty array in the state 我如何在react js中更改对象数组的状态 - How do i change state of an array of objects in react js 如何在反应 redux 减速器 state 中更改数组中的 object 值? - How can I change an object value in an array in react redux reducer state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM