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?
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.
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' .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With