每次执行toggleTest函数时,使用钩子将状态从true切换为false都是如此:
const [testToggler, setTest] = useState(false);
const toggleTest = () => {
setTest(!testToggler);
console.log(testToggler);
}
但是当我将其作为对象发送时,它不会切换。怎么样?
const [testToggler, setTest] = useState({ label: 'test', val: false });
const toggleTest = () => {
setTest({label: 'test', val: !testToggler} );
console.log(testToggler);
}
答案 0 :(得分:4)
function getLayersData()
{
var lyrs = [];
var layers = 1;
while (true)
{
ref = new ActionReference();
ref.putIndex(charIDToTypeID('Lyr '), layers);
try{var desc = executeActionGet(ref);}catch(err){alert(err);break;};
var lyr = {};
lyr.type = desc.getInteger(stringIDToTypeID("layerKind"));
lyr.name = desc.getString(charIDToTypeID("Nm "));
lyr.id = desc.getInteger(stringIDToTypeID("layerID"));
if (lyr.type == layerType && lyr.name.match(layerName))
{
var adj = desc.getList(stringIDToTypeID("adjustment")).getObjectValue(0);
if (adj.hasKey(stringIDToTypeID("color")))
{
var curColor = new SolidColor();
curColor.rgb.red = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("red"));
curColor.rgb.green = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("grain"));
curColor.rgb.blue = adj.getObjectValue(stringIDToTypeID("color")).getUnitDoubleValue(stringIDToTypeID("blue"));
lyr.color = curColor;
if (lyr.color.rgb.hexValue == currentColor[0])
{
lyrs.push(lyr);
};
};
};
layers++;
};
return lyrs;
};
是一个对象,所以testToggler
始终为false。
您可能需要!testToggler
属性。