Expected an assignment or function call and instead saw an expression
Expected an assignment or function call and instead saw an expression
Can anyone advise where I’m going wrong with this? Should I set my condition up differently etc?
5 Answers 5
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You are misusing the conditional operator as an if statement, that’s why you are getting that note. The real work in the code is done as a side effect of the expression, and the result of the expression is ignored.
As a real if statement, it would be:
You can use the conditional operator if you use it as an actual expression:
In general, for disabling the ‘Expected an assignment or function call and instead saw an expression.’ warning, you can do /* jshint expr: true */
Does it pass if you write it like this?
Linters and hinters usually don’t like in/decrements because they’re sensitive to bugs.
Expected an assignment or function call and instead saw an expression
I’m totally cool with this JSLint error. How can I tolerate it? Is there a flag or checkbox for it?
You get it when you do stuff like:
Both do the same exact thing. If you put:
into the minifier it minifies down to this anyway:
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I don’t think JSLint has an option to turn that off.
JSHint (a fork with more options) has an option for it, though: The expr option, documented as «if ExpressionStatement should be allowed as Programs».
Expected an assignment or function call and instead saw an expression.eslintno-unused-expressions
Hi I am using a ternary operator inside a function. But eslint throws error on this. Pls help me to fix this.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If your current code works, it would probably be more appropriate to avoid the conditional operator entirely and use if / else instead:
This way, you don’t actually have any unused expressions, which is the right way to fix this sort of linter warning.
You can simply add this allowTernary comment above the ternary operator expression, to disable eslint error for that line:
add option to your eslint to allowTernary = true
You can add it as a comment above the line or in eslint config where you have your rules defined
React: Expected an assignment or function call and instead saw an expression
I am trying to fix this lint error at line const def = (props) => < in following sample code.
Any idea why i am getting this lint error?
11 Answers 11
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You are not returning anything, at least from your snippet and comment.
This is not returning anything, you are wrapping the body of the arrow function with curly braces but there is no return value.
Expected an assignment or function call and instead saw an expression.
I had this similar error with this code:
To correct all I needed to do was add parenthesis around the curved brackets
The return statements should place in one line. Or the other option is to remove the curly brackets that bound the HTML statement.
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.
Possible way is (sure you can change array declaration to getting from db or another external resource):
You use a function component:
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:
In my case the problem was the line with default instructions in switch block:
The fault is within your if statement. Had same error some time ago. I got to noticed that Within my ternary operator, I was having lines of code sepereted from each other by commas, changed to using if statement still was having same error.
I corrected it by sepereting the expressions and giving each a seperate if statement (works with ternary operator too) but in the end, I was having too many redundant codes. annoying. Have not found any solution since then
Firstly you must have at least one «return» before your parent div tag in your function as follows
Or, you can use an Arrow function in a single line as:
In this case «return» is not compulsory.
Secondly, you should use «Conditional (ternary) Operator».
In my case the eslint error was caused by an unexpected use case. Hence the code was correct, but eslint failed:
eslint finds an expression which is generated, but not used. That the generation produces a result (potential exception) is beyond the analyzer.
In defense of eslint : I don’t expect the analyzer to anticipate this side effect. And it’s foremost usecase (prevent you from unfinished code-lines) is important. So nobody to blame..
I’d suggest to just disable the warning linewise like this:
expected assignment or function call: no-unused-expressions ReactJS
I am new to React and for this code its giving this error
Dont understand where getting wrong, please help
17 Answers 17
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
This happens because you put bracket of return on the next line. That might be a common mistake if you write js without semicolons and use a style where you put opened braces on the next line.
Interpreter thinks that you return undefined and doesn’t check your next line. That’s the return operator thing.
In my case I had curly braces where it should have been parentheses.
Where it should have been:
The reason for this, as explained in the MDN Docs is that an arrow function wrapped by () will return the value it wraps, so if I wanted to use curly braces I had to add the return keyword, like so:
For me the error occured when using map. And I didn’t use the return Statement inside the map.
Above code produced error.
Simply adding return solved it.
If you’re using JSX inside a function with braces you need to modify it to parentheses.
In my case i never put return inside a arrow function so my code is follow
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
Working OK. No Error
In my case I used commas instead of semicolons in constructor.
Example with errors:
instead I should have used semicolons like underneath:
I encountered the same error, with the below code.
Above issue got resolved, when I changed braces to parentheses, as indicated in the below modified code snippet.
In my case it is happened due to braces of function if you use JSX then you need to change braces to parentheses:
I tried this but not work, don’t know why
But when I change Curly Braces to parentheses and Its working fine for me
Hopefully it will help you too.
Solution: Move the start of opening bracket.
When you use the return keyword, just make sure that the START of the bracket is ON THE SAME LINE as the return keyword. If you don’t do this, you will get a bug.
In my case I have got this error, because used a call inside of the condition without a semicolon:
I changed it, and the error has gone:
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.
like so. In above code sample you can see there is only opening parenthesis at the left of the function callback.
Getting Expected an assignment or function call and instead saw an expression no-unused-expressions error when trying to render a component in React
I am trying to render Card component based on the number of items in the items object but I am getting the error on line 6.
And this is the Card component :
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Or in a Cleaner Way:
As the first answer, you need to return the JSX in map()
Or using ( ) instead of <> inside the map()
Not the answer you’re looking for? Browse other questions tagged javascript reactjs or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Expected an assignment or function call and instead saw an expression error
Having a slight issue with some code that I have written, I keep getting the error:
expected an assignment or function call and instead saw an expression
I have commented where the error is happening. Not sure what the issue is however. Code is as follows:
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Because you are not doing anything inside forEach (no assignment, no calculation), also forEach will not return anything, use map to return the TableCell for each array entry.
Eslint will throw an error, if you write a forEach loop without any operation.
Why Am I Getting An Expected an Assignment or Function Call and Instead Saw an Expression?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can address this via changing your code in the componentDidMount :
or if you do not want to set the state:
The main reason for this error is ESLint and more specifically the Ternary rules specified here.
This is not allowed by the Disallow Unused Expressions rule and its allowTernary option:
This however is allowed:
This solved it perfectly, thank you for all the help.
Ternary operators can’t be used like you did instead do it like below
JavaScript : Expected and assignment or function call and instead saw an expression
I am using JSHint to ensure my JavaScript is «strict» and I’m getting the following error:
Expected an assignment or function call and instead saw an expression
On the following code:
Any ideas why I’m getting such an error or how I can code to remove the error.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Here is a simplified version that gives the same warning:
Expected an assignment or function call and instead saw an expression
This means that you have an expression but do not assign the result to any variable. jshint doesn’t care about what the actual expression is or that there are side effects. Even though you assign something inside of the expression, you are still ignoring the result of the expression.
There is another error by jslint if you care about it:
Unexpected assignment expression
This warns you that you may want to use == instead of = inside logical expressions. It’s a common error, therefore you are discouraged to use assignments in logical expressions (even though it is exactly what you want here).
Basically, jshint/jslint do not like misuse of shortcut evaluation of logical operator as replacement for if statements. It assumes that if the result of an expression is not used, it probably shouldn’t be an expression.
React error [Expected an assignment or function call and instead saw an expression]
Today i’m trying to make a Sidebar by following a tutorial after many packages.
So tutorial link can be found at youtube here
Here is my code who react don’t like:
Line 24:25: Expected an assignment or function call and instead saw an expression no-unused-expressions
Search for the keywords to learn more about each error.
I don’t really know the error and how to fix it.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Your map function should return JSX code to work. When you are using curly braces you should use the keyword return or simply return the code by wrapping it inside parentheses.
Not the answer you’re looking for? Browse other questions tagged javascript reactjs or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Expected an assignment or function call and instead saw an expression
I’m totally cool with this JSLint error. How can I tolerate it? Is there a flag or checkbox for it?
You get it when you do stuff like:
Both do the same exact thing. If you put:
into the minifier it minifies down to this anyway:
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I don’t think JSLint has an option to turn that off.
JSHint (a fork with more options) has an option for it, though: The expr option, documented as «if ExpressionStatement should be allowed as Programs».
Expected an assignment or function call and instead saw an expression
I’m totally cool with this JSLint error. How can I tolerate it? Is there a flag or checkbox for it?
You get it when you do stuff like:
Both do the same exact thing. If you put:
into the minifier it minifies down to this anyway:
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I don’t think JSLint has an option to turn that off.
JSHint (a fork with more options) has an option for it, though: The expr option, documented as «if ExpressionStatement should be allowed as Programs».
Why Am I Getting An Expected an Assignment or Function Call and Instead Saw an Expression?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You can address this via changing your code in the componentDidMount :
or if you do not want to set the state:
The main reason for this error is ESLint and more specifically the Ternary rules specified here.
This is not allowed by the Disallow Unused Expressions rule and its allowTernary option:
This however is allowed:
This solved it perfectly, thank you for all the help.
Ternary operators can’t be used like you did instead do it like below
Expected an assignment or function call and instead saw an expression on ternary
Currently getting Expected an assignment or function call and instead saw an expression no-unused-expressions
I tried returning an empty div at the end of my ternary operator but still get the same error.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
It is complaining about how you use a ternary instead of using an if/else statement to do the work. The code is expecting
If you want to use a ternary you need to do it with the objects, since only thing that is different is the type, use it on the type.
You need not return anything from useEffect in this use case. Also useEffect will not render anything as it is a hook in place to perform side effects.
React: Expected an assignment or function call and instead saw an expression
I am trying to fix this lint error at line const def = (props) => < in following sample code.
Any idea why i am getting this lint error?
11 Answers 11
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You are not returning anything, at least from your snippet and comment.
This is not returning anything, you are wrapping the body of the arrow function with curly braces but there is no return value.
Expected an assignment or function call and instead saw an expression.
I had this similar error with this code:
To correct all I needed to do was add parenthesis around the curved brackets
The return statements should place in one line. Or the other option is to remove the curly brackets that bound the HTML statement.
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.
Possible way is (sure you can change array declaration to getting from db or another external resource):
You use a function component:
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:
In my case the problem was the line with default instructions in switch block:
The fault is within your if statement. Had same error some time ago. I got to noticed that Within my ternary operator, I was having lines of code sepereted from each other by commas, changed to using if statement still was having same error.
I corrected it by sepereting the expressions and giving each a seperate if statement (works with ternary operator too) but in the end, I was having too many redundant codes. annoying. Have not found any solution since then
Firstly you must have at least one «return» before your parent div tag in your function as follows
Or, you can use an Arrow function in a single line as:
In this case «return» is not compulsory.
Secondly, you should use «Conditional (ternary) Operator».
In my case the eslint error was caused by an unexpected use case. Hence the code was correct, but eslint failed:
eslint finds an expression which is generated, but not used. That the generation produces a result (potential exception) is beyond the analyzer.
In defense of eslint : I don’t expect the analyzer to anticipate this side effect. And it’s foremost usecase (prevent you from unfinished code-lines) is important. So nobody to blame..
I’d suggest to just disable the warning linewise like this:
I am using JSLint to ensure my JavaScript is «strict» and I’m getting the following error:
Expected an assignment or function call and instead saw an expression
On the following code:
Any ideas why I’m getting such an error? Also, I’m using jQuery as seen in the above code, in case that makes a difference.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
My guess would be that JSLint is unhappy since you’re using the ternary operator, and you’re not doing anything with the value. Refactoring this into the equivalent:
would eliminate the error. If for some reason you’re really attached to using the ternary operator, the «right» way to use it would be:
I believe this is because the ternary operator evaluates the expression and returns a value that is expected to be assigned. For example:
However, you are using it like a regular if/then/else statement. While the ternary operator does perform if/then/else, it is traditionally used in assignents.
EDIT: As an addendum: would this statement make sense to you?
you are using an expression (an expression using the ternary operator to be precise) in a single line: your line is comprised uniquely of an expression.
this is considered poor programming practice in many language, and could be rewritten using an if statement to render this line more clear.
Just asked the same Q without finding this one for some reason.
The ternary returns a value that isn’t being used so you’re abusing the ternary structure by not using the value (even though the function calls are being made as intended).
Expected an assignment or function call and instead saw an expression error
Having a slight issue with some code that I have written, I keep getting the error:
expected an assignment or function call and instead saw an expression
I have commented where the error is happening. Not sure what the issue is however. Code is as follows:
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Because you are not doing anything inside forEach (no assignment, no calculation), also forEach will not return anything, use map to return the TableCell for each array entry.
Eslint will throw an error, if you write a forEach loop without any operation.
Line 7:9: Expected an assignment or function call and instead saw an expression no-unused-expressions
My Code is this and when i run the code it gives me below error
Line 7:9: Expected an assignment or function call and instead saw an expression no-unused-expressions
I tried what I know for this but it not solved
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
I think the problem is that you put your jsx in the function `MainContent without returning anything.
Wrap your jsx inside return statement
Not the answer you’re looking for? Browse other questions tagged reactjs or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
JavaScript : Expected and assignment or function call and instead saw an expression
I am using JSHint to ensure my JavaScript is «strict» and I’m getting the following error:
Expected an assignment or function call and instead saw an expression
On the following code:
Any ideas why I’m getting such an error or how I can code to remove the error.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Here is a simplified version that gives the same warning:
Expected an assignment or function call and instead saw an expression
This means that you have an expression but do not assign the result to any variable. jshint doesn’t care about what the actual expression is or that there are side effects. Even though you assign something inside of the expression, you are still ignoring the result of the expression.
There is another error by jslint if you care about it:
Unexpected assignment expression
This warns you that you may want to use == instead of = inside logical expressions. It’s a common error, therefore you are discouraged to use assignments in logical expressions (even though it is exactly what you want here).
Basically, jshint/jslint do not like misuse of shortcut evaluation of logical operator as replacement for if statements. It assumes that if the result of an expression is not used, it probably shouldn’t be an expression.
JavaScript : Expected and assignment or function call and instead saw an expression
I am using JSHint to ensure my JavaScript is «strict» and I’m getting the following error:
Expected an assignment or function call and instead saw an expression
On the following code:
Any ideas why I’m getting such an error or how I can code to remove the error.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Here is a simplified version that gives the same warning:
Expected an assignment or function call and instead saw an expression
This means that you have an expression but do not assign the result to any variable. jshint doesn’t care about what the actual expression is or that there are side effects. Even though you assign something inside of the expression, you are still ignoring the result of the expression.
There is another error by jslint if you care about it:
Unexpected assignment expression
This warns you that you may want to use == instead of = inside logical expressions. It’s a common error, therefore you are discouraged to use assignments in logical expressions (even though it is exactly what you want here).
Basically, jshint/jslint do not like misuse of shortcut evaluation of logical operator as replacement for if statements. It assumes that if the result of an expression is not used, it probably shouldn’t be an expression.
I keep getting the error message «Expected an assignment or function call and instead saw an expression» in my code, I think it is because I have an if inside my map but I have tried a few ways and I cant seem to make the code work.
I am trying to display information returned from my API, but I only want to display messages that have been sent when the ID length is equal to 7.
Example of the information returned from my API
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You need to add return after your condition.
By the way, it’s also better to explicitly handle the else case, by returning null for instance.
You cannot mix javascript and jsx this way
When not used as an arrow function body, the following is not a proper javascript instruction, but an expression (has a value but does nothing, so it cannot be the body of an if statement):
Just add a return for making it an expression (as expected in an if block) and allow the code to compile properly:
You should use conditional rendering for this cases:
Expected an assignment or function call and instead saw an expression on ternary
Currently getting Expected an assignment or function call and instead saw an expression no-unused-expressions
I tried returning an empty div at the end of my ternary operator but still get the same error.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
It is complaining about how you use a ternary instead of using an if/else statement to do the work. The code is expecting
If you want to use a ternary you need to do it with the objects, since only thing that is different is the type, use it on the type.
You need not return anything from useEffect in this use case. Also useEffect will not render anything as it is a hook in place to perform side effects.
How to fix error: Expected an assignment or function call and instead saw an expression no-unused-expressions
I still have no idea how to fetch data from my action.
Here is the action:
And also tried to use ES5:
and I have an error to get data to Redux in Component:
I have got the following error:
Expected an assignment or function call and instead saw an expression no-unused-expressions
How can I fix this?
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
You need to call your action using dispatch keywoard. If you want to use dispatch keyword from a react component then you need to wrap your Component with connect() function from react-redux package.
Next you will need to define at least mapStateToProps function to get data (EDIT: you do not need to include mapStateToProps 🙂 include it only when you want to get some data back to your component), which you saved into the store by your reducer, from your redux store into your component as props.
So your code should looks like following:
you should be carefull with the syntax—
Not the answer you’re looking for? Browse other questions tagged reactjs redux or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Expected an assignment or function call and instead saw an expression no-unused-expressions in React?
Line 3:1: Expected an assignment or function call and instead saw an expression no-unused-expressions. This is my first time using ‘use restrict» in javascript and I don’t know how to fix the rule that is against.
My Nav.js file in react
1 Answer 1
To invoke strict mode for an entire script, put the exact statement «use strict»; (or ‘use strict’; ) before any other statements.
Since you put that string literal after your import s, it is just a string literal expression. It doesn’t trigger strict mode. It doesn’t do anything (because you don’t assign it / pass it / etc).
Move it so it is the very first line of your JS.
Expected an assignment or function call and instead saw an expression.eslintno-unused-expressions
Hi I am using a ternary operator inside a function. But eslint throws error on this. Pls help me to fix this.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
If your current code works, it would probably be more appropriate to avoid the conditional operator entirely and use if / else instead:
This way, you don’t actually have any unused expressions, which is the right way to fix this sort of linter warning.
You can simply add this allowTernary comment above the ternary operator expression, to disable eslint error for that line:
add option to your eslint to allowTernary = true
You can add it as a comment above the line or in eslint config where you have your rules defined
Function to map an object throws «Expected an assignment or function call and instead saw an expression»
I have this function below on TypeScript and I cannot compile due to the following error:
Expected an assignment or function call and instead saw an expression
Even searching on Stack Overflow, I don’t really understand why yet.
2 Answers 2
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Keep in mind that map is meant to transform an array, you have to return a value for each item in the callback. I changed it to forEach in the example below:
and alternative would be to use filter and map :
Getting «Expected an assignment or function call and instead saw an expression no-unused-expressions» on ReactJS
I am newbie programming on React and I am getting an error when I try to compile my program. The problem occurs because I am trying to execute one method if a condition happens when I click a button and other (from the parent class) if not. I tried with multiple solutions but I wasn’t able to make the program work.
This is the child class:
This is the parent class:
1 Answer 1
I think this might be the problem:
If you’re trying to invoke the function you’d want to change it to:
I don’t see any reason this needs to be in state in the first place. If you’re just going to invoke the function handed in as a prop, you can do that directly:
Not the answer you’re looking for? Browse other questions tagged reactjs or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Expected an assignment or function call and instead saw an expression
The «Expected an assignment or function call and instead saw an expression» error is thrown when JSLint, JSHint or ESLint encounters an expression with no effect. In the following example we have a conditional expression that will evaluate to true but has no other effect on the program:
Why do I get this error?
This error is raised to highlight a piece of useless and unnecessary code. The code will work as expected but since a lone floating expression has no effect on anything there is no point in it being there at all.
In general you would expect to see a statement which has an effect, such as assigning a value to a variable or invoking a function:
About the author
This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.
This project is supported by orangejellyfish, a London-based consultancy with a passion for JavaScript. All article content is available on GitHub under the Creative Commons Attribution-ShareAlike 3.0 Unported licence.
Have you found this site useful?
Please consider donating to help us continue writing and improving these articles.
Expected an assignment or function call and instead saw an expression
Last updated: Apr 9, 2022
(React) 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.
Here are 2 examples of how the error occurs.
In the App component, the error is caused in the Array.map() method.
The issue here is that we aren’t returning anything from the callback function we passed to the map() method.
To solve the error, we have to either use an explicit return statement or implicitly return a value using an arrow function.
We solved the issue in our map() method by explicitly returning. This is necessary because the Array.map method returns an array containing all of the values that were returned from the callback function we passed to it.
An alternative approach is to use an implicit return with an arrow function.
We used an implicit arrow function return for the App component.
If we are using an implicit return to return an object, we have to wrap the object in parenthesis.
When used without parenthesis, you have a block of code, not an object.
If you believe that the eslint rule should not be causing an error in your scenario, you can turn it off for a single line, by using a comment.
The comment should be placed right above the line where the error is caused.





























