【发布时间】:2019-11-10 12:40:09
【问题描述】:
学习 React Native。试图将字符串转换为函数名。收到错误消息“func 不是函数。func 未定义”。需要一些帮助。最终目标是创建一个具有背景颜色并具有用于增加/减少 RGB 值的按钮的视图
const [red, setRed] = useState(0);
const [green, setGreen] = useState(0);
const [blue, setBlue] = useState(0);
const setColor = (color, change) => {
// color === "red", "green", "blue"
// change === +10, -10
const colorCapitalized = color.charAt(0).toUpperCase() + color.slice(1);
var stateMethod = 'set' + colorCapitalized;
var func = window[stateMethod];
if (color + change > 255 || color + change < 0) {
return;
} else {
func(color + change);
}
};
【问题讨论】: