105
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

vikash chander
  • 49
  • 1
  • 10
Prateek Pandey
  • 1,077
  • 2
  • 8
  • 14

18 Answers18

179

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

Community
  • 1
  • 1
Andrey Medvedev
  • 1,981
  • 1
  • 7
  • 7
  • 1
    The JavaScript parser will automatically add a semicolon when, during the parsing of the source code, it finds these particular situations: 1. when the next line starts with code that breaks the current one (code can spawn on multiple lines). 2. when the next line starts with a }, closing the current block 3. when the end of the source code file is reached 4. when there is a return statement on its own line 5. when there is a break statement on its own line 6. when there is a throw statement on its own line 7. when there is a continue statement on its own line. – elcordova Jan 29 '20 at 19:54
  • I got the same error. But I did not use return() there. how can i resolve that? – Aadi Nov 25 '21 at 08:34
110

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>
}
Ashwani Panwar
  • 3,819
  • 3
  • 46
  • 66
Gus
  • 15,397
  • 7
  • 25
  • 28
  • 3
    This was the issue for me – Badrush Oct 04 '19 at 16:27
  • 4
    One whole year after I started to learn web development, I never once realised that arrow functions occasionally needed `return`.. I need to go look over my old code and maybe I'll see why certain things weren't working. Especially in react.. Thanks for actually providing this answer! – LiveBacteria Dec 05 '19 at 04:35
  • This solutions worked and I understand now why it was giving me error. – Ipsita Dec 01 '20 at 17:34
41

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.

Ritesh Jung Thapa
  • 1,177
  • 2
  • 13
  • 20
14

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>;
        ));
isherwood
  • 58,414
  • 16
  • 114
  • 157
Yazan Najjar
  • 1,830
  • 16
  • 10
4

In my case i never put return inside a arrow function so my code is follow

`<ProductConsumer>
   {(myvariable)=>{
      return  <h1>{myvariable}</h1>
   }}
</ProductConsumer> `
Krishna Jangid
  • 4,961
  • 5
  • 27
  • 33
3

In my case I used commas instead of semicolons in constructor.

Example with errors:

class foo(bar1, bar2, bar3){
    this.bar1=bar1,
    this.bar2=bar2,
    this.bar3=bar3,

}

instead I should have used semicolons like underneath:

class foo(bar1, bar2, bar3){
    this.bar1=bar1;
    this.bar2=bar2;
    this.bar3=bar3;
}
tabaluga
  • 31
  • 2
2

In my case the error happened because the new line after the return statement.

Error : Expected an assignment or function call and instead saw an expression

return
(
    <ul>
        {
            props.numbers.map(number => <li key={number.toString()}>number</li>)
        }
    </ul>
);

Working OK. No Error

return (
    <ul>
        {
            props.numbers.map(number => <li key={number.toString()}>number</li>)
        }
    </ul>
);
Namig Hajiyev
  • 1,117
  • 15
  • 16
2

I encountered the same error, with the below code.

    return this.state.employees.map((employee) => {
      <option value={employee.id}>
        {employee.name}
      </option>
    });

Above issue got resolved, when I changed braces to parentheses, as indicated in the below modified code snippet.

      return this.state.employees.map((employee) => (
          <option value={employee.id}>
            {employee.name}
          </option>
        ));
isherwood
  • 58,414
  • 16
  • 114
  • 157
Shravan Ramamurthy
  • 3,896
  • 5
  • 30
  • 44
2

In my case it is happened due to braces of function if you use JSX then you need to change braces to parentheses:

const [countries] = useState(["USA", "UK", "BD"])

I tried this but not work, don't know why

 {countries.map((country) => {
        <MenuItem value={country}>{country}</MenuItem>
  })}

But when I change Curly Braces to parentheses and Its working fine for me

  {countries.map((country) => ( //Changes is here instead of {
        <MenuItem value={country}>{country}</MenuItem>
  ))} //and here instead of }
             

Hopefully it will help you too...

isherwood
  • 58,414
  • 16
  • 114
  • 157
shaheb
  • 557
  • 5
  • 12
2

Solution: Move the start of opening bracket...

Move the start of the bracket

When you use the return keyword, just make sure that the START of the bracket is ON THE SAME LINE as the return keyword. If you don't do this, you will get a bug.

BenKoshy
  • 33,477
  • 14
  • 111
  • 80
2

In my case I replaced , with ;

Change from this

<Button
    onClick={() => {
        function1(), function2();
    }}>
    Click
</Button>

to this

<Button
    onClick={() => {
        function1();
        function2();
    }}>
    Click
</Button>
Anjan Talatam
  • 2,212
  • 1
  • 12
  • 26
1
import React from 'react';

class Counter extends React.Component{
    state = {
        count: 0,
    };

    formatCount() {
        const {count} = this.state;
        // use a return statement here, it is a importent,
        return count === 0 ? 'Zero' : count;
    }
    render() {
        return(
          <React.Fragment>
              <span>{this.formatCount()}</span>
              <button type="button" className="btn btn-primary">Increment</button>
          </React.Fragment>
        );
    }
}

export default Counter;
rene
  • 41,474
  • 78
  • 114
  • 152
Mamunur Rashid
  • 1,095
  • 17
  • 28
  • 3
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – rene Jun 01 '19 at 11:39
1

In my case I have got this error, because used a call inside of the condition without a semicolon:

  private async _setActive(active: boolean) {
    if (this.isActive === active) {
      return;
    }
    this.isActive = active;

    this.isActive ? this._start() : this._stop();
  }

I changed it, and the error has gone:

  private async _setActive(active: boolean) {
    if (this.isActive === active) {
      return;
    }
    this.isActive = active;

    if (this.isActive) {
      await this._start();
    } else {
      this._stop();
    }
  }
jocoders
  • 1,594
  • 2
  • 19
  • 54
1

The error - "Expected an assignment or function call and instead saw an expression no-unused-expressions" comes when we use curly braces i.e {} to return an object literal expression. In such case we can fix it with 2 options

  1. Use the parentheses i.e ()
  2. Use return statement with curly braces i.e {}

Example :

const items = ["Test1", "Test2", "Test3", "Test4"];
console.log(articles.map(item => { `this is ${item}` })); // wrong
console.log(items.map(item => (`this is ${item}`))); // Option1
console.log(items.map(item => { return `this is ${item}` })); // Option2
Naresh Nagpal
  • 299
  • 4
  • 6
1

In case someone having a problem like i had. I was using the parenthesis with the return statement on the same line at which i had written the rest of the code. Also, i used map function and props so i got so many brackets. In this case, if you're new to React you can avoid the brackets around the props, because now everyone prefers to use the arrow functions. And in the map function you can also avoid the brackets around your function callback.

props.sample.map(function callback => (
   ));

like so. In above code sample you can see there is only opening parenthesis at the left of the function callback.

Rafia Zafar
  • 363
  • 1
  • 13
0

Instead of

 return 
 (
  <div>
    <h1>The Score is {this.state.speed};</h1>
  </div>
 )

Use Below Code

 return(
   <div>
     <h1>The Score is {this.state.speed};</h1>
   </div>
  )

Basically use brace "(" in the same line of return like "return(". It will fix this issue. Thanks.

Joee
  • 1,834
  • 18
  • 19
0

In my case, I got the error on the setState line:

increment(){
  this.setState(state => {
    count: state.count + 1
  });
}

I changed it to this, now it works

increment(){
  this.setState(state => {
    const count = state.count + 1

    return {
      count
    };
  });
}
Simran Bhake
  • 117
  • 8
0

Just want to add my solution in case anyone else is experiencing this error. In my case it didn't have anything to do with brackets, an arrow function or a missing return statement. I had a check like this

if (this.myProperty) {
    this.myProperty == null
}

I had to remove this then the error went away. It was difficult to figure this out because the error message was not at all descriptive.

SendETHToThisAddress
  • 2,756
  • 7
  • 29
  • 54