0

I am trying to iterate a Redux array into a Material UI datagrid and I keep getting errors that my array is empty every time I try to render. The store data loads correctly without the iterating loop however it returns empty whenever I try to populate the table.

Would I best creating a custom method for this or is there an easier fix? I have used this route before but it was on a simpler component.

Error: UserList(...): Nothing was returned from render. This usually means a return statement is missing. Or, to render nothing, return null.
const columns = [
        { field: 'id', headerName: 'ID', width: 70 },
        { field: 'firstname', headerName: 'First name', width: 130 },
        { field: 'lastname', headerName: 'Last name', width: 130 },
        { field: 'userName', headerName: 'Username', width: 130},
        { field: 'userEmail', headerName: 'Email', width: 180 },
        { field: 'userRole', headerName: 'Role', width: 80}
    ];
    const rows = [
        { id: userLists.userID, lastName: userLists.surname, firstname: userLists.firstName, userName: userLists.username, userEmail: userLists.userEmail, userRole: userLists.userRoleID },
    ];

    return (

        <div style={{ height: 400, width: '100%' }}>
        {userLists.length > 0 &&
         userLists.map(( {userLists}) => {
            <DataGrid rows={rows} columns={columns} pageSize={5} checkboxSelection />
     })}
        </div>
    );
}

1 Answer 1

1

This is a common mistake using arrow functions, you are missing the return in your map call, or have mistaken the implicit return () with the block expression {}:

{  // checking the array's length is redundant =P,
   // make sure the array has something, though:
   userLists?.map(( {userLists}) => {
       // missing return here: 
       return <DataGrid rows={rows} columns={columns} pageSize={5} checkboxSelection />
     }
   )
}

There is even a tweet about it =P.

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

Comments

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.