Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spread syntax ES6 with statement

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 : {}}},
like image 902
Palaniichuk Dmytro Avatar asked Jul 19 '17 08:07

Palaniichuk Dmytro


People also ask

What is spread syntax in ES6?

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.

How do you write a spread operator?

Syntax: var variablename1 = [... value]; In the above syntax, … is spread operator which will target all values in particular variable.

What is spread operator in JavaScript with example?

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.


1 Answers

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 : {})},
like image 57
Bergi Avatar answered Sep 25 '22 13:09

Bergi