reactjs - 具有动态(基于 uid)路径的 React-Redux mapStateToProps
问题描述
我是 React 和 React-Redux 的新手。我正在尝试根据用户 ID 使用动态路径传递“mapStateToProps”。使用的基本代码如下:
const mapStateToProps = (state) => {
console.log(state);
console.log(state.firebase.auth.uid); <===== THIS DISPLAYS THE USERS ID, PROVING THE PATH EXISTS
return {
courses: `state.firestore.ordered.${state.firebase.auth.uid}`, <======= THIS LINE IS AN ERROR EVEN THOUGH THE PATH EXISTS
}
}
如果我手动输入用户 ID,代码可以正常工作。假设用户 ID 是“12345”,然后将该行替换为
courses: state.firestore.ordered.12345, <======= THIS LINE WOULD WORK
任何关于为什么这不起作用的澄清以及使这项工作的替代方法将不胜感激!
解决方案
如果要访问 JS 对象中的动态属性,则需要使用方括号
courses: state.firestore.ordered[state.firebase.auth.uid]
推荐阅读
- asp.net-core - 如何在 ASP.NET Core 中获取 HttpContext.Current.Session?
- google-apps-script - 在当前工作簿中没有的工作表上运行 Apps 脚本 - Google Sheets / Apps 脚本
- mongodb - mongodb 在聚合中使用 $exist?
- css - 使用 CSS 的全宽滑块标题图像的焦点位置
- django - 如何在生产环境中记录 Django 警告和错误以记录文件?
- python - Django 管理命令不刷新标准输出
- javascript - Redux - ReactJS 应用程序不会重新渲染(尽管 JSON.parse 用于新对象)
- python - 无法将浮点值从 python 返回到 HTML
- ruby-on-rails - 使用字符串强参数更新 Rails 模型的整数字段
- mapbox - 风格/卫星 - 是否有可能在不同的缩放级别上有两种不同的美学?