Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

reactjs

jsx

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

const propTypes = { prop1: PropTypes.string, prop2: PropTypes.string, prop3: PropTypes.string, prop4: PropTypes.string, prop5: PropTypes.string, }  const abc = (props) => { some code here }  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>  }; 

Any idea why i am getting this lint error?

like image 325
User7354632781 Avatar asked Aug 08 '17 16:08

User7354632781


People also ask

How do you ignore expected an assignment or function call and instead saw an expression?

The React. js error "Expected an assignment or function call and instead saw an expression" occurs when we forget to return a value from a function. To solve the error, make sure to explicitly use a return statement or implicitly return using an arrow function.

Is not defined react JSX no undef?

The React. js error "X is not defined react/jsx-no-undef" occurs when we forget to import a function, class or a variable in our code before using it. To solve the error, make sure to import the value before using it in your code, e.g. import {myFunc} from 'my-package' .

Are not valid as a react child?

The React. js error "Objects are not valid as a React child" occurs when we try to directly render an object or an array in our JSX code. To solve the error, use the map() method to render arrays or access properties on the object in your JSX code.


1 Answers

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.

like image 180
G4bri3l Avatar answered Sep 20 '22 21:09

G4bri3l