How to update state from Backend NextJs

Viewed 52

I'm using useState in the frontend.

And what I want to do is to update this state from the backend. The thing is, in the backend I have a Loop, and I don't know how to trigger the setState for each loop, since I can't pass the setState as a parameter, and I saw that the hooks only works on the frontend(browser).

Structure in the frontend

const [imageLinks, setImageLinks] useState([])


useEffect ( () => {}, [imageLinks])

return (
  {
    imageLinks.map((link) {
     <img src={link} />
    }
  }
)

Structure in the Api endpoint

function myFunction (cids, y, z) {
  for (let i = 0; i < cids. length; i+) {
    if (cids[i]) {
      nftTower [i] = await tokenMinterFon(cids[i])
      let imageLink = await axios.get('https://infura-ipfs.io' + 
       cids[i])

// Correct format of the link

...Some logic here 

     let imageLinksFiltered = imageLinks.filter (imageLink =>
     imageLink !== correctFormatLink
      )
      imageLinks = imageLinksFiltered

      // Here's where I want to trigger at each loop and change the state of what I have in the frontend
      setImageLinks(imageLinksFiltered);

I have an Image attached for a better understanding of what I'm doing.

enter image description here

Thank you for your help and explanation.

0 Answers
Related