expected assignment or function call: no-unused-expressions ReactJS

前端 未结 14 795
挽巷
挽巷 2020-12-07 16:26
class Game extends Component 
{
  constructor() 
  {
    super()
    this.state = {
      speed: 0
    }
    //firebaseInit()
  }
  render()
  {
    return 
    (
           


        
相关标签:
14条回答
  • 2020-12-07 17:11

    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
    
    0 讨论(0)
  • 2020-12-07 17:16

    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
        };
      });
    }
    
    0 讨论(0)
提交回复
热议问题