React Expected an assignment or function call and instead saw an expression
You need to return a value at each pets.map
iteration, currently you’re returning undefined.
return pets.map((pet, index) => {
return (
<div key={index}>
<h3>{pet.name}</h3>
<p>{pet.species}</p>
</div>
)
});
Line 7:9: Expected an assignment or function call and instead saw an expression no-unused-expressions
I think the problem is that you put your jsx in the function `MainContent without returning anything.
To fix this do:
function MainContent() {
return (
<put all the jsx here>
)
}
2022 React - Expected an assignment or function call and instead saw an expression
Your useEffect dependency array is in the wrong place, I think that's what is causing it.
should be:
useEffect (() => {
setChartData({
labels: data.webs.data.map((t) => t.attributes.severity),
datasets: [
{
label: "Price in USD",
data: data.webs.data.map((t) => t.attributes.id),
backgroundColor: [
"#ffbb11",
"#ecf0f1",
"#50AF95",
"#f3ba2f",
"#2a71d0"
]
}
]
})
}, [data])
Also ther is a .
on the line you have the arrow on but I'm not sure id it's a copy paste typo or is it actually in your code
Function to map an object throws "Expected an assignment or function call and instead saw an expression"
You are using a new javascript feature called Optional chaining which is only supported since typescript 3.7
. Make sure you have at least version 3.7
or newer in your react project.
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:
const myFunction = (list: IListType | undefined) =>{
let listpush: AType[]=[];
list?.item.forEach(it => {
if(it.values && it.values?.key && it.values?.value){
listpush.push({
attr1: it.name,
attr2: it.values.key,
attr3: it.values.value,
})
}
})
return listpush;
}
and alternative would be to use filter
and map
:
const myFunction = (list: IListType | undefined): AType[] => {
if(!list){
return [];
}
return list.item.filter(it => {
return it.values && it.values?.key && it.values?.value;
}).map(item => ({
attr1: it.name,
attr2: it.values.key,
attr3: it.values.value,
}))
}
Related Topics
Html Anchor Tag With JavaScript Onclick Event
How to Update Component State on Redux State Change
Document.Queryselector Always Returns Null
How to Remove Image from Browser Cache
How to Put a Variable in an Src Attribute
How to Remove a Parameter from a Url
Detect If Page Is Load from Back Button
Disabled Button Onclick Event Fires
How to Do Validation for 10 Digit Mobile Number Using Jquery
Why Is It a Bad Practice to Return Generated HTML Instead of Json or Is It
Hosting Your Own JavaScript Scripts Files (Other Than Jquery) on Fast Free Cdns Like Google
How to Get List of All Timezones in JavaScript
Jquery Validation Plugin - Typeerror: $(...).Validate Is Not a Function
Get an Attribute from the Clicked Element With JavaScript
How to Export or Convert Json to Excel in Angularjs
When Click on Menu, It Opens, When Click Again It Close
Javascript Random on Array Without Repeat
Calculate the Bounding Box'S X, Y, Height and Width of a Rotated Element Via JavaScript