你好,我正在尝试更新我在React中的状态,但是我不知道为什么它总是进入未定义selectedUser的选项。我尝试将所有内容放入if-else中,但得到的结果相同,奇怪的是,当我在控制台中编写某些内容时,它进入了我的条件中,但取而代之的是空值。
find
selectedUser现在是假值
var vale_data = [{
service: "allocateorder",
vol_slo: "10000"
},
{
service: "cancelorder",
vol_slo: "10000"
},
{
service: "adviceorder",
vol_slo: "10000"
}
];
var sli_data = [{
service: "allocateorder",
vol_sli: "0.9484949"
},
{
service: "cancelorder",
vol_sli: "0.242322"
}
];
var result = vale_data.map(element => {
let sli = sli_data.find(service => service["service"] === element["service"]) ||
{ vol_sli: "" };
return { ...element,
...sli
};
});
console.log(result);
当我获得所选用户的控制台日志时
const [inputFieldState, setInputFieldState] = useState(InputFields(selectedUser));
function InputFields(selectedUser: any) {
return selectedUser
? selectedUser
: {
email: "",
firstName: "",
lastName: "",
customerId: "",
id: "",
role: ""
};
}
答案 0 :(得分:0)
根据评论,selectedUser
是一个函数
const [inputFieldState, setInputFieldState] = useState(InputFields(selectedUser()));
function InputFields(user: any) { // just changed the name to avoid potential confusion
return user
? user
: {
email: "",
firstName: "",
lastName: "",
customerId: "",
id: "",
role: ""
};
}
答案 1 :(得分:0)
我认为您需要将useState(InputFields(selectedUser))更改为
useState(() => InputFields(selectedUser))
以便它不会在每个渲染中使用初始状态