简体   繁体   中英

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 chaining here instead of having this complex nested thing. How can I re write it? I am unable to understand even after going through the MDN doc.

How can one rewrite this with optional chaining?

Try

(eventsubcategoryvalue) ? ( data.body.spending.requestspend === 'y' ? data.body.spending.allowspend : chargeamount ) : chargeamount 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM