问题
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 : {}}},
回答1:
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 : {})},
来源:https://stackoverflow.com/questions/45184666/spread-syntax-es6-with-statement