简体   繁体   中英

.map function not rendering react data

I have a function that calls an API, and returns an array when 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.

 {
    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>
      )
    })
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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