问题描述
我的组件中有两个 useEffects。据我所知,它们是按照列出的顺序执行的。在我的第二个 useEffect 中,我对第一个中创建的值进行了更改,当我调出该值时,它会在我的 return 语句之前更新,但是当我在渲染中使用该值时,它显示为未定义..谁能解释为什么这会发生吗?
const [entries,setEntries] = useState([]);
useEffect(() => {
const loadEntries = async (base,token) => {
let result = await api.UserRecords.chart(userID,token);
if (result.pagetoken) {
// another page of entries exist,load the next page
loadEntries(base.concat(result.items),result.pagetoken);
} else {
// received last page of entries
let allEntries = base.concat(result.items);
if (isPccIntegrated) {
try {
// for PCC integrated patients,pull any progress note from PCC and add it into the chart items
const pccNotes = await api.PCC.progressNotes(userID);
const notes = (pccNotes.Notes || []).map((note) => ({
// refactor PCC notes to partially match the existing mdBox format
isPCCNote: true,orgId: note.orgId,orgUuid: note.orgUuid,facId: note.facId,progressNoteId: note.progressNoteId,description: note.progressNoteType || "",createdAt: note.createdDate,}));
// merge the mapped PCC notes into all patient notes
allEntries = allEntries.concat(notes);
// restort the entries by creation date,lastest first (descending order)
allEntries.sort((a,b) => {
if (moment(a.createdAt).isBefore(b.createdAt)) {
return 1;
} else if (moment(a.createdAt).isAfter(b.createdAt)) {
return -1;
}
return 0;
});
} catch (err) {}
}
// loading is complete
setEntries(allEntries);
setLoaded(true);
}
};
loadEntries([]);
},[isPccIntegrated]);
useEffect(() => {
const loadAllProviders = async () => {
//Todo: Make conditional if entries exists
try {
//get all providers
const results = await api.Providers.listnoAudit({ scope: "all" });
const allProviders = results.items;
//map over the entries to get providerId's out
const providerIDs = entries.map((entry) => entry.createdBy);
//map over response to get out provider names and id's
const idName = allProviders.map((provider) => [provider.id,provider.fullName]);
//find if any returned providers have matching ids to the ones in entries
const matches = idName.filter((match) => providerIDs.includes(match[0]));
//Todo: map over entries
// if matches[0] = entries.createdby then
//push matches[1] into the object at the index where match occurs
entries.map((entry,idx) => {
matches.map((provider) => {
if (entry.createdBy === provider[0]) {
let en = { ...entry,fullName: provider[1] };
entries.splice(idx,1,en);
}
});
return entries;
});
} catch (err) {}
};
loadAllProviders();
},[entries]);
...
//this one returns the entries with the fullName value
console.log(entries)
return (
{entries.map((entry,idx) => (
<p>{entry.fullName}</p>
)
)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)