我需要设置一个与useSelector(redux)一起使用的变量,看起来很简单,但是我不知道该怎么做。
const CATEGORY = cat;
console.log(CATEGORY); // correctly get the strings 'food' or 'design'
const getItems = useSelector(state => state.appDb.CATEGORY);
console.log(getItems); // I get undefined
基本上,我想使用该变量进行设置:
state.appDb.food
或state.appDb.design
正确的语法是什么?
答案 0 :(得分:0)
您需要使用brackets notation
来访问动态对象键,如
const CATEGORY = cat;
console.log(CATEGORY); // correctly get the strings 'food' or 'design'
const getItems = useSelector(state => state.appDb[CATEGORY]);
console.log(getItems);