react-native - React Native - 为 useSelector 设置一个简单的变量
问题描述
我需要设置一个变量以与 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
什么是正确的语法?
解决方案
所有你需要用来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);
推荐阅读
- java - 使用 servlet 和数据库限制登录尝试
- php - 当我输入错误密码时,此 PDO 登录表单也会记录我。如何解决?
- apache-spark - 将容量调度程序从默认更改为 ambari 上的 DominantResourceCalculator 后无法重新启动 spark2 thriftserver、spark-shell、sparksql
- mysql - SELECT 列表的表达式 #3 不在 GROUP BY 子句中,并且在尝试创建视图时包含非聚合列
- javascript - Paypal SDK 问题得到错误 [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
- python - 单个 FMU 的多核仿真
- java - Java DTO 和最终字段
- ffmpeg - 使用 FFMPEG 计算的 SSIM 值不是我所期望的
- requirejs - 如何使用 requireJS 加载车把
- xml - For each in XSLT 仅循环通过第一条记录