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;
}
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.
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' .
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.
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>
))}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With