我正在尝试使用React Hooks,但是不知何故我的状态没有更新
const [Choices, setChoices] = useState([]);
useEffect(() => {
async function getUsers() {
// Here’s the magic
let tempChoices = [];
const promises = MAINLIST.List.ChoicesFields.map(async z => {
tempChoices[z] = [];
let GetChoicesFromInternalFieldResults = await GetChoicesFromInternalField(
z
);
GetChoicesFromInternalFieldResults.map(c => {
tempChoices[z].push({
key: c,
text: c,
value: c
});
});
});
await Promise.all(promises);
const object = { Choices: tempChoices };
// THIS IS PRINTING CORRECT VALUES
console.log(object);
setChoices(object);
}
getUsers();
}, []);
这是console.log结果
但是当我在开发人员工具中检查状态时,状态为空
答案 0 :(得分:0)
我找到了答案。我将“ tempChoices”放入数组而不是对象。
const [Choices, setChoices] = useState({});
useEffect(() => {
async function getUsers() {
// Here’s the magic
let tempChoices = {};
const promises = MAINLIST.List.ChoicesFields.map(async z => {
tempChoices[z] = [];
let GetChoicesFromInternalFieldResults = await GetChoicesFromInternalField(
z
);
GetChoicesFromInternalFieldResults.map(c => {
tempChoices[z].push({
key: c,
text: c,
value: c
});
});
});
await Promise.all(promises);
const object = { Choices: tempChoices };
setChoices(object);
}
getUsers();
}, []);