1

I have a users list that is correctly fetched from the API as I can see the records in my console. However, when I try to map it and display it as a table, it doesn't work. This is my component:

interface IUser {
    id: number,
    email: string,
    username: string,
    password:string

}

interface UsersProps {
    user: IUser[],
    url?: string,
    loading: boolean
}


const Users: React.FC<UsersProps> = ({
    user,
    loading,
    url 
}) => {

    const [users, setUsers] = useState<UsersProps>({
        user:[],
        loading: true,
       
        });


    const request: RequestInit = {
        method: 'GET',
        body: JSON.stringify(user),
        headers: {
            'Content-Type':'application/json'
            }
    }

   

    useEffect(() => {
        fetch(url, request).then(response => response.json()).then(data => {
            setUsers(prev => ({
                ...prev,
                user: [...prev.user, data],
                loading:false
                }) )
            console.log(users.user, "users")
            console.log("url", url)
        })
    }, [])

    const getUsers = (u: IUser[]) => {
        console.log("users table", u)
        return (
            <table>
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Password</th>
                    </tr>
                </thead>
                <tbody>
                    {u.map((user, index) =>
                        <tr key={index}>
                            <td>{user.email}</td>
                            <td>{user.password}</td>
                            <td>{user.username}</td>
                          
                        </tr>
                        
                    )}
                </tbody>
            </table>
            )
    }

    users.user.map((user) => {
        console.log("uswe o", user)
    })


    const rendering = () => {
        return (
            users.loading ?
                <div>Is loading...</div> :
                 getUsers(users.user) 
            
        )
    }
       
    //console.log("get users", getUsers(users.user))
   
    return (
    
        <div>
            {rendering() }
        </div>
       
        )
}


export default Users;

I don't understand what I'm doing wrong, since the values should be displayed.screenshot

4
  • No errors in the console? Commented Dec 31, 2022 at 0:15
  • No, everything looks good. Commented Dec 31, 2022 at 0:19
  • Maybe I missed something but this component seems to be getting data correctly, although it seems to receive another loading as props from parent, so any chance there could be another conditionally rendered loading from parent that might be preventing this component to output? Not sure if it's related, the loading message in this component seems to be "Is loading...", but the screenshot seems to be displaying "Loading", which seems to be not matching somehow. Commented Dec 31, 2022 at 0:40
  • @JohnLi I've removed any other component from rendering except for this one. It's still not working. I've tried to just write dummy values inside the table to see if they are rendered and they are. I thought that they might not be visible but that's not the case. Commented Dec 31, 2022 at 0:48

1 Answer 1

2

I assume that you get an array from your fetch. So I think you should spread the data like that :

useEffect(() => {
    fetch(url, request).then(response => response.json()).then(data => {
        setUsers(prev => ({
            ...prev,
            user: [...prev.user, ...data],
            loading:false
         }));
    })
}, [])

With your code it seems that users.user is an array of an array then you can't try to access email, password and username.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, you are correct, now it works. I can't believe it was that easy. Thank you so much for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.