Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expected an assignment or function call and instead saw an expression error

Tags:

Having a slight issue with some code that I have written, I keep getting the error:

expected an assignment or function call and instead saw an expression

I have commented where the error is happening. Not sure what the issue is however. Code is as follows:

renderBody() {
    const { bookings } = this.props;
    if (bookings.length > 0) {
        return (
            Object.keys(bookings).map(e => (
                <TableBody>
                    {Object.values(bookings[e]).forEach((value) => {
                        <TableCell>{value}</TableCell>; //<-ERROR??
                    })}
                </TableBody>
            ))
        );
    }
    return null;
}
like image 510
Mubeen Hussain Avatar asked Apr 18 '18 13:04

Mubeen Hussain


People also ask

How do you solve error expected an assignment or function call and instead saw an expression?

js error "Expected an assignment or function call and instead saw an expression" occurs when we forget to return a value from a function. To solve the error, make sure to explicitly use a return statement or implicitly return using an arrow function.

Is not defined react JSX no undef?

The React. js error "X is not defined react/jsx-no-undef" occurs when we forget to import a function, class or a variable in our code before using it. To solve the error, make sure to import the value before using it in your code, e.g. import {myFunc} from 'my-package' .

Are not valid as a react child?

The React. js error "Objects are not valid as a React child" occurs when we try to directly render an object or an array in our JSX code. To solve the error, use the map() method to render arrays or access properties on the object in your JSX code.


1 Answers

Because you are not doing anything inside forEach (no assignment, no calculation), also forEach will not return anything, use map to return the TableCell for each array entry.

Eslint will throw an error, if you write a forEach loop without any operation.

Write it like this:

{Object.values(bookings[e]).map(value => (
    <TableCell>{value}</TableCell>
))}
like image 113
Mayank Shukla Avatar answered Oct 11 '22 16:10

Mayank Shukla