【发布时间】:2019-10-12 08:04:34
【问题描述】:
您好,我正在尝试更新我在 React 中的状态值,但我不知道为什么它总是转到未定义 selectedUser 的选项。我尝试将所有内容都放在 if-else 中,但得到了相同的结果,奇怪的是,当我在控制台中编写某些内容时,它进入了我的条件,但它取而代之的是空值。
const [inputFieldState, setInputFieldState] = useState(InputFields(selectedUser));
function InputFields(selectedUser: any) {
return selectedUser
? selectedUser
: {
email: "",
firstName: "",
lastName: "",
customerId: "",
id: "",
role: ""
};
}
selectedUser 现在是一个假值
return {
firstName: planes[Math.floor(Math.random() * planes.length)],
lastName: planes[Math.floor(Math.random() * planes.length)],
customerId: planes[Math.floor(Math.random() * planes.length)],
id: planes[Math.floor(Math.random() * planes.length)],
role: names[Math.floor(Math.random() * names.length)],
email: Math.ceil(Math.random() * 5)
};
当我对所选用户进行控制台日志时,我得到了
{firstName: "x-wing", lastName: "Winnebago", customerId: "y-wing", id: "TIE-fighter", role: "Anakin", …}
customerId: "y-wing"
email: 2
firstName: "x-wing"
id: "TIE-fighter"
lastName: "Winnebago"
role: "Anakin"
【问题讨论】:
-
你从哪里获得 selectedUser 的价值?
-
selectedUser 是我创建的假值
-
return { firstName: planes[Math.floor(Math.random() * planes.length)], lastName: planes[Math.floor(Math.random() * planes.length)], customerId:planes[Math.floor(Math.random() * planes.length)],id:planes[Math.floor(Math.random() * planes.length)],角色:names[Math.floor(Math. random() * names.length)],电子邮件:Math.ceil(Math.random() * 5) };
-
为什么selectedUser会有回报?
标签: reactjs typescript