I tried to write ternary operator with spread syntax and copy two objects. Is it possible to use ternary operator with spread syntax inside with literal objects? My code works okay, I just want to optimize it.
hintStyle: disabled ? {...globalStyles.hint, ...globalStyles.hintDisabled} : globalStyles.hint,
I want to write like this:
hintStyle: {...globalStyles.hint, {disabled ? ...globalStyles.hintDisabled : {}}},
Spread syntax ( ... ) allows an iterable, such as an array or string, to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected.
Syntax: var variablename1 = [... value]; In the above syntax, … is spread operator which will target all values in particular variable.
The spread operator is a new addition to the set of operators in JavaScript ES6. It takes in an iterable (e.g an array) and expands it into individual elements. The spread operator is commonly used to make shallow copies of JS objects. Using this operator makes the code concise and enhances its readability.
Spread is not an operator, it's part of the object literal syntax (or at least it will be when the proposal is accepted). You need to write
{...globalStyles.hint, ...(disabled ? globalStyles.hintDisabled : {})},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With