[英]Why is my React Hook State not updating when using setState?
I'm trying to create a simple form where a user enters basic information which gets posted to a database on submit.我正在尝试创建一个简单的表单,用户可以在其中输入基本信息,这些信息会在提交时发布到数据库中。 I check if my customer already exists in my database and if not I create a new customer.
我检查我的客户是否已存在于我的数据库中,如果不存在,我将创建一个新客户。 I'm new to React Native and React Hooks so maybe I'm violating one of the rules here, but when I pass the information into setCustomer to update the state nothing happens.
我是 React Native 和 React Hooks 的新手,所以我可能违反了这里的规则之一,但是当我将信息传递给 setCustomer 以更新 state 时,什么也没有发生。 Debugging show me the correct values are being passed to setCustomer.
调试显示正确的值正在传递给 setCustomer。 Any help would be appreciated.
任何帮助,将不胜感激。
const initialCustomerState = { email: "", firstName: "", lastName: ""}
export default function SendRequest(){
const customers = getAllCustomers()
const [customer,setCustomer] = useState(initialCustomerState)
const {
control,
handleSubmit,
formState: {errors, isValid}
} = useForm({mode: 'onBlur'})
const onSubmit = data => {
if(checkIfExists(data.email) == false){
let email = data.email
let firstName = data.firstName
let lastName = ''
if(data.hasOwnProperty('lastName')){
lastName = data.lastName
}
setCustomer(email,firstName,lastName)
createNewCustomer()
createNewReview(data.email)
}else{
createNewReview(data.email)
}
}
Your customer value is an object, you should place email,firstName,lastName
inside curly brackets {email,firstName,lastName}
您的客户价值是 object,您应该将
email,firstName,lastName
放在花括号{email,firstName,lastName}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.