Questions tagged [optional-chaining]

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null. Use for questions about such chaining with a appropriate language tag like [swift], [JavaScript], etc.

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null.

JavaScript Optional chaining operator - ?.

173 questions
-1
votes
1 answer

How to use optional chaining for deeply nested values in react where ternary operator used for conditional rendering?

I have this deeply nested value eventsubcategoryvalue: data.body.spending.requestspend==='y' ? data.body.spending.allowspend : chargeamount so here eventsubcategoryvalue is decided on basis of result of the ternary operator. I want to use optional…
-1
votes
2 answers

Cannot read properties of null (reading 'length') in reactJs

I have been stuck on this error for an hour now and nothing i do seems to resolve it. What am i missing? Shouldn't the optional chaining on the array maping resolve this issue?

Cart Items

noob_coder
  • 21
  • 6
-1
votes
3 answers

Can't implement simple optional chaining

I am trying to implement a simple optional chaining state update. What I want is for the items to ONLY be changed IF the item is defined/exists; if not/is undefined, I want the item to keep the previous state (e.g userID should remain = 2 if not…
-2
votes
1 answer

Syntax highlighting is not working in VS Code with the Babel extension because of optional chaining

As you can see in the picture, my syntax highlighting stops working just after I used an optional chaining in my JSX file. Please let me know how can I fix the problem. enter image description here I am using Babel ES6/ES7 in my VS Code.
-2
votes
1 answer

React - Why Optional chaining is not working for this axios request

Im getting this error for the code below: Uncaught TypeError: Cannot read properties of undefined (reading '0') I tried to use optional chaining but it's not working... const [deliveredOrders, setDeliveredOrders] = useState([]); useEffect(() => { …
zuZuu
  • 87
  • 1
  • 8
-2
votes
2 answers

Not sure conceptwise where I am wrong in optional chaining in swift..Can anyone please explain why the output is that?

class ElectionPoll { var candidate: Pollbooth? } class Pollbooth { var name = "MP" } let cand = ElectionPoll() if let candname = cand.candidate?.name { print("Candidate name is \(candname)") } else { print("Candidate name cannot be…
Vyankatesh
  • 139
  • 1
  • 10
-2
votes
1 answer

Property doesn't exist on type 'object' when using optional chaining

There is no way that this code fails const fn = (obj: object) => obj?.prop; but the TypeScript compiler prevents me from writing that, explaining that Property 'prop' does not exist on type 'object' but that's precisely the use of optional…
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
-3
votes
1 answer

?. in Typescript?

Is ?. a valid operator in Typescript or Javascript? I have seen code like below: const var1 = obj1?.innerObj1?.somePropVal; When I try this in the chrome console, I get an error if obj1 is undefined. Not sure whether this kind of code would…
cethie
  • 49
  • 3
  • 8
1 2 3
11
12