同时获取不同服务器以创建一个组件

huangapple go评论59阅读模式
英文:

Fetching diferent servers at the same time to create a component

问题

I'm working on a modular system that makes sure it has access to the other servers on my front-end app. I'm fetching all of them inside a forEach loop and then trying to update the useState() just adding the "new component". What am I doing wrong?

In less words, trying to insert the new component at the end of the previously created ones.

const [dropdownItems, setdropdownItems] = useState([])

session.reachablePartner.forEach((serverPartner) => {
    fetch(`http://${serverPartner.ipAddress}/api/isonline`).then((res) => {
        setdropdownItems(...dropdownItems, 
            <DropDownItem
                key={serverPartner.ipAddress}
                onClick={() => {
                    window.location.replace(`http://${serverPartner.ipAddress}/`)
                }}
            >
                {serverPartner.serverName}
            </DropDownItem>
        )
    }).catch(() => {
        console.log(serverPartner.serverName + " Is not reachable")
    })
})
英文:

I'm working on a modular system that makes sure it has access to the other servers on my front end app. I'm fetching all of them inside a forEach loop and then trying to update the useState() just adding the "new component". What Am I doing wrong?

In less words, trying to insert the new component at the end of the previous created ones.

const [dropdownItems, setdropdownItems] = useState([])

session.reachablePartner.forEach((serverPartner)=&gt;{
    fetch(`http://${serverPartner.ipAddress}/api/isonline`).then((res)=&gt;{
      setdropdownItems(...dropdownItems,&lt;DropDownItem
        key={serverPartner.ipAddress}
        onClick={() =&gt; {
          window.location.replace(`http://${serverPartner.ipAddress}/`)
          
        }}
      &gt;
        {serverPartner.serverName}
      &lt;/DropDownItem&gt;)
    }).catch(()=&gt;{
      console.log(serverPartner.serverName + &quot; Is not reachable&quot;)
    })
  })

答案1

得分: 1

我现在已经让它工作了,以下是最终的代码,如果有人需要的话。

代码:

 useEffect(() => {
    
    (async() => {
    
    
      const promises = session.reachablePartner.map((serverPartner) => {
        return fetch(`http://${serverPartner.ipAddress}/api/isonline`).then(reply => reply).catch(err => console.log(err))
      })
    
    
      Promise.all(promises).then(result => {
    
        const linkstoShow = session.reachablePartner.filter((link,index) => result[index])
        setdropdownItems(linkstoShow.map(serverPartner => (
          <DropDownItem
            key={serverPartner.ipAddress}
            onClick={() => {
              window.location.replace(`http://${serverPartner.ipAddress}/`)
            }}
          >
            {serverPartner.serverName}
          </DropDownItem>
        )))
      })
    })()
    
  }, [])
英文:

I got it working now, here is the final code if some one needs it.

Code:

 useEffect(()=&gt;{

    (async()=&gt;{


      const promises = session.reachablePartner.map((serverPartner)=&gt;{
        return fetch(`http://${serverPartner.ipAddress}/api/isonline`).then(reply=&gt;reply).catch(err=&gt;console.log(err))
      })


      Promise.all(promises).then(result=&gt;{

        const linkstoShow = session.reachablePartner.filter((link,index)=&gt;result[index])
        setdropdownItems(linkstoShow.map(serverPartner=&gt;{
          &lt;DropDownItem
          key={serverPartner.ipAddress}
          onClick={() =&gt; {
            window.location.replace(`http://${serverPartner.ipAddress}/`)
            
          }}
        &gt;
          {serverPartner.serverName}
        &lt;/DropDownItem&gt;
        }))
      })
    })()

  },[])

huangapple
  • 本文由 发表于 2023年7月14日 04:22:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定