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

时间:2020-05-28 09:50:42

标签: 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 undefined

基本上,我想使用该变量进行设置:

state.appDb.foodstate.appDb.design

正确的语法是什么?

1 个答案:

答案 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);