简体   繁体   English

.map 函数不呈现反应数据

[英].map function not rendering react data

I have a function that calls an API, and returns an array when console.logged.我有一个调用 API 的函数,并在 console.logged 时返回一个数组。 Use effect is running, and return the array normally but it never updates the front end.使用效果正在运行,并正常返回数组但它永远不会更新前端。 Have been working on this for a while, and haven't found a solution.已经研究了一段时间,还没有找到解决方案。 Am I missing something?我错过了什么吗?

useEffect(()=>{
        const fetchData = async (user) => {
            console.log(user)
            const { data, error } = await supabase
                .from('contacts')
                .select()
                .match({ user_id: user })
            return data;
        }
        const user = supabase.auth.user().id;
        fetchData(user).then((data)=> {
            // console.log(data)
            setContacts(data);
            console.log(contacts)
        }).catch(error=>{
            console.log(error)
        })
        // returns an array or objects. each object is a contact    
    },[contacts])
    return(
        <div>
            <h1>All Contacts</h1>
            <tbody>
                <tr>
                    <th>Name</th>
                    <th>email</th>
                    <th>phone</th>
                    <th>notes</th>
                </tr>
                {
                    contacts.map((contact, i) => {
                        <tr key={i}>
                            <td>{contact.prosepct_name}</td>
                            <td>{contact.prospect_email}</td>
                            <td>{contact.prospect_phone}</td>
                            <td>{contact.notes}</td>
                        </tr>
                    })
                }
                
            </tbody>

On first glance - it looks like you need to wrap the inner part of your map function in a return.乍一看 - 看起来您需要将 map 函数的内部部分包装在 return 中。

 {
    contacts.map((contact, i) => {
      return (
        <tr key={i}>
          <td>{contact.prosepct_name}</td>
          <td>{contact.prospect_email}</td>
          <td>{contact.prospect_phone}</td>
          <td>{contact.notes}</td>
        </tr>
      )
    })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM