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

前端 未结 8 1259
一个人的身影
一个人的身影 2020-11-27 05:40

I am trying to fix this lint error at line const def = (props) => { in following sample code.

const propTypes = {
prop1: PropTypes.string,
pr         


        
相关标签:
8条回答
  • 2020-11-27 06:01

    Possible way is (sure you can change array declaration to getting from db or another external resource):

    const MyPosts = () => {
    
      let postsRawData = [
        { id: 1, text: 'Post 1', likesCount: '1' },
        { id: 2, text: 'Post 2', likesCount: '231' },
        { id: 3, text: 'Post 3', likesCount: '547' }
      ];
    
      const postsItems = []
      for (const [key, value] of postsRawData.entries()) {
        postsItems.push(<Post text={value.text} likesCount={value.likesCount} />)
      }
    
      return (
          <div className={css.posts}>Posts:
              {postsItems}
          </div>
      )
    }
    
    0 讨论(0)
  • 2020-11-27 06:04

    In my case the problem was the line with default instructions in switch block:

      handlePageChange = ({ btnType}) => {
        let { page } = this.state;
        switch (btnType) {
          case 'next':
            this.updatePage(page + 1);
            break;
          case 'prev':
            this.updatePage(page - 1);
            break;
          default: null;
        } 
      }
    

    Instead of

    default: null;
    

    The line

    default: ;
    

    worked for me.

    0 讨论(0)
  • 2020-11-27 06:13

    Expected an assignment or function call and instead saw an expression.

    I had this similar error with this code:

    const mapStateToProps = (state) => {
        players: state
    }
    

    To correct all I needed to do was add parenthesis around the curved brackets

    const mapStateToProps = (state) => ({
        players: state
    });
    
    0 讨论(0)
  • 2020-11-27 06:14

    Not sure about solutions but a temporary workaround is to ask eslint to ignore it by adding the following on top of the problem line.

    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
    
    0 讨论(0)
  • 2020-11-27 06:16

    You are not returning anything, at least from your snippet and comment.

    const def = (props) => { <div></div> };

    This is not returning anything, you are wrapping the body of the arrow function with curly braces but there is no return value.

    const def = (props) => { return (<div></div>); }; OR const def = (props) => <div></div>;

    These two solutions on the other hand are returning a valid React component. Keep also in mind that inside your jsx (as mentioned by @Adam) you can't have if ... else ... but only ternary operators.

    0 讨论(0)
  • 2020-11-27 06:16

    You use a function component:

    const def = (props) => {
    <div>
    <div className=" ..some classes..">{abc}</div>
    <div className=" ..some classes..">{t('translation/something')}</div>
    
    <div ...>
    <someComponent 
     do something
    />
    
    if (some condition) {
    do this
    } else {
    do that
    }
    
     </div>
    
    };
    

    In the function component, you have to write a return or just add parentheses. After the added return or parentheses your code should look like this:

    const def = (props) => ({
    <div>
    <div className=" ..some classes..">{abc}</div>
    <div className=" ..some classes..">{t('translation/something')}</div>
    
    <div ...>
    <someComponent 
     do something
    />
    
    if (some condition) {
    do this
    } else {
    do that
    }
    
     </div>
    });
    
    0 讨论(0)
提交回复
热议问题