The error “Expected an assignment or function call and saw an expression instead” is thrown when JSLint, JSHint, or ESLint encounters an invalid expression. This error is raised to highlight a piece of code that is useless and unnecessary. The code should work as expected but since a single floating expression doesn’t affect anything there’s no sense there.
How to identify errors “Expected an assignment or function call and instead saw an expression”
This error occurs when you wrap the function body with curly braces ({}) but do not return any value. Here are some of the displays you might see:
{SidebarData.map((item,index) => { <li key={index} className={item.cName}> <Link to={item.path}> {item.icon} <span>{item.title}</span> </Link> </li> })}
when displaying fullcode:
Error with SidebarData.js items:
import React from 'react'; import {X, Tag, FileEarmarkPostFill,PersonBadge,CashStack,Tools} from 'react-bootstrap-icons' export const SidebarData = [ { title: 'Administration', path: '/admin', icon: <X />, cName: 'nav-text' }, { title: 'Category', path: '/admin/category', icon: <Tag />, cName: 'nav-text' }, { title: 'Product', path: '/admin/product', icon: <FileEarmarkPostFill />, cName: 'nav-text' }, { title: 'Order', path: '/admin/order', icon: <CashStack />, cName: 'nav-text' }, { title: 'User', path: '/admin/user', icon: <PersonBadge />, cName: 'nav-text' }, { title: 'Support', path: '/admin/support', icon: <Tools />, cName: 'nav-text' }, ]
How to solution the error
For quick resolution, You should know how to use both () and {}. We already know that parentheses are used to pass parameters in functions and curly braces are used to indicate the scope of something. However, in ES6, parentheses can also be used for return elements. So you can see that (some jsx) is the same as {return(some jsx)}.
For example, the below code will work when use the keyword “return” or simply return the code by enclosing it inside parentheses.
{SidebarData.map((item,index) => ( <li key={index} className={item.cName}> <Link to={item.path}> {item.icon} <span>{item.title}</span> </Link> </li> ))}
or the other way:
{SidebarData.map((item,index) => return { <li key={index} className={item.cName}> <Link to={item.path}> {item.icon} <span>{item.title}</span> </Link> </li> })}
Conclusion
In general, resolving the error “Expected an assignment or function call and instead saw an expression” is indeed not difficult. Hope our above article is useful for you. If there is still something that cannot be resolved, please do not hesitate to contact us!
Related articles
- Simple solution to correct the requests.exceptions.ConnectionError: (‘Connection aborted.’ RemoteDisconnected(‘Remote end closed connection without response’)) issue
Python is a popular programming language that can be used widely in a lot of applications. Python is also a good choice as a programming language depending on user background and perspective. Because it is used widely and popular, if you find any errors when using Python. It is a common problem, you face the […]
- “[Errno 61] Connection refused” is occurring even, the program is connecting with the port well and the socket is running in the interfaces.
If you see the “[Errno 61] Connection refused” issue although you checked the program, port, socket and interfaces. Although your program of Python works well in the server and the client, they are installed at the same device. The local IP from my device is connecting with the clients but this IP is not connected […]
- Description “Return by Reference”.
C++ is considered not only as a language of Object Oriented Programming, but also an intermediate level language. It identifies both high and low level languages. It became easy and widely used in computer programs and that is the reason why we should understand the definition and its function as well. Such as Return by […]
- How to use correctly “useMemo vs. useEffect + useState”
Hello everyone, today I will talk about useMemo in Reactjs. Talk about Reactjs programmers, you’ve probably used React hooks, specifically here useMemo, it’s quite familiar but not everyone understands and uses it properly so… Today we will learn its usage in this case “useMemo vs. useEffect + useState”. Let’s started! Difference between “useMemo vs. useEffect […]
- Definition about Basic Math Functions.
Java, which is an important programming language, is a popular program computer in the world. Java is also used for: mobile applications (especially Android apps; desktop applications; web applications; web servers and application servers; database connection; and much more. Above the key information of Java, we have the answer for why we use Java. Moreover, […]