微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的值在我控制台出来后在我的渲染中是未定义的,它被显示出来

如何解决我的值在我控制台出来后在我的渲染中是未定义的,它被显示出来

我的组件中有两个 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 举报,一经查实,本站将立刻删除。