I updated my react version to 18 from version 17, after that this code –> data?.area?.width=['111','220']
is throwing error like this "The left-hand side of an assignment expression must be a variable or a property access.", what will be the solution?
what are the code changes required here?
3
Answers
You haven’t provided enough code information but as per this line (
data?.area?.width=['111','220']
) I understand that you want to check for condition when thearea
has width 111 or 220, so for this purpose this will be the condition(data?.area?.width===111 || data?.area?.width===220)
That is not a valid syntax to assign the field value with optional chaining (
?.
).You have to check both
data
anddata.area
are truthy then assign the value as below:It interprets same as
The problem is because
data?.area?.width=['111','220']
is not valid JS. The react version does not matter.data?.area?.width
is not valid on the left hand side of an assignment operator.Your work around, unfortunately, is:
A proposal was raised for this and TC39 has taken the decision to not
include it.
Here is the discussion link
There are complications to introducing the feature, some of them being:
Then do we execute the method
c
if a is nullish/undefined, or not.There are a few other viewpoints mentioned in the thread. Can go through them to understand the decision better. But you can expect this feature to not be introduced in JS in near future.