expected assignment or function call: no-unused-expressions ReactJS
class Game extends Component
{
constructor()
{
super()
this.state = {
speed: 0
}
//firebaseInit()
}
render()
{
return
(
<div>
<h1>The Score is {this.state.speed};</h1>
</div>
)
}
}
export default Game;
I am new to React and for this code its giving this error
Expected an assignment or function call and instead saw an expression no-unused-expressions
Dont understand where getting wrong, please help
This happens because you put bracket of return
on the next line. That might be a common mistake if you write js without semicolons and use a style where you put opened braces on the next line.
Interpreter thinks that you return undefined and doesn't check your next line. That's the return
operator thing.
Put your opened bracket on the same line with the return
.
In my case I had curly braces where it should have been parentheses.
const Button = () => {
<button>Hello world</button>
}
Where it should have been:
const Button = () => (
<button>Hello world</button>
)
The reason for this, as explained in the MDN Docs is that an arrow function wrapped by ()
will return the value it wraps, so if I wanted to use curly braces I had to add the return
keyword, like so:
const Button = () => {
return <button>Hello world</button>
}
For me the error occured when using map. And I didn't use the return Statement inside the map.
{cart.map((cart_products,index) => {
<span>{cart_products.id}</span>;
})};
Above code produced error.
{cart.map((cart_products,index) => {
return (<span>{cart_products.id}</span>);
})};
Simply adding return solved it.
If you're using JSX inside a function with braces you need to modify it to parentheses.
Wrong Code
return this.props.todos.map((todo) => {
<h3> {todo.author} </h3>;
});
Correct Code
//Change Curly Brace To Paranthesis change {} to => ()
return this.props.todos.map((todo) => (
<h3> {todo.author} </h3>;
));