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

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


        
相关标签:
14条回答
  • 2020-12-07 16:50

    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.

    0 讨论(0)
  • 2020-12-07 16:52

    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.

    0 讨论(0)
  • 2020-12-07 16:53

    In my case it is happened due to curly braces of function if you use jsx then you need to change curly braces to Parentheses, see below code

    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...

    0 讨论(0)
  • 2020-12-07 16:55

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

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

    `<ProductConsumer>
       {(myvariable)=>{
          return  <h1>{myvariable}</h1>
       }}
    </ProductConsumer> `
    
    0 讨论(0)
提交回复
热议问题