React native React Native-为useSelector设置一个简单变量

React native React Native-为useSelector设置一个简单变量,react-native,react-redux,React Native,React Redux,我需要设置一个与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 un

我需要设置一个与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


正确的语法是什么?

访问动态对象键所需的全部

  const CATEGORY = cat;
  console.log(CATEGORY); // correctly get the strings 'food' or 'design'

  const getItems = useSelector(state => state.appDb[CATEGORY]);
  console.log(getItems);

您确定state.appDb.CATEGORY有值吗?尝试在console.log(getItems)yes之前添加console.log(state)<代码>控制台日志(类别);//正确获取字符串“food”或“design”您确定state.appDb.CATEGORY==CATEGORY吗?我不明白这一点,我只知道CATEGORY会返回字符串“food”或“design”是的,这没关系,但问题是您描述的getItems未定义,所以如果state.appDb.CATEGORY未定义,基本上可能会出现这种情况,这就是为什么要求console.log(state)(不仅仅是CATEGORY)