【问题标题】:How to call a mapStateToProps path with numerous dynamic variables (React-Redux)如何调用具有大量动态变量的 mapStateToProps 路径(React-Redux)
【发布时间】:2020-07-30 16:21:32
【问题描述】:

不确定正确的语法是什么,但我试图获取状态中找到的 courseTitle 变量的值(字符串),如下所示(当输入 console.log(状态):

状态
|_____ 火库
|______________ 数据
|____________________“用户 ID”
|_____________________________“课程 ID”
|________________________________________ 课程名称

“userID”和“courseID”是动态变量,其值会根据登录的用户和用户选择的课程而变化。 courseTitle 是一个包含课程标题的变量,是通过 mapStateToProps 读取的内容。

【问题讨论】:

    标签: dynamic react-redux state mapstatetoprops


    【解决方案1】:

    注意:解决方案
    在编写问题时,我能够测试一些东西并最终找到解决方案。不过,我认为在这里分享我的代码可能会对未来遇到类似问题的程序员有所帮助。

    const mapStateToProps = (state, ownProps) => {
    
        const UID = state.firebase.auth.uid;                 <------THIS IS WHAT THE DYNAMIC "userID" is.
        const courseID = state.CreateContent.selectedCourse; <------THIS IS WHAT THE DYNAMIC "courseID" is.
    
        const courses = state.firestore.data[UID];
        const course = courses ? courses[courseID] : null;
        const courseTitle = course ? course.courseTitle : null;
        console.log('Title: ' + courseTitle);                 <------ THIS DISPLAYS THE CORRECT courseTitle.
    
        return {
    
            courseTitle: courseTitle,
    
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 1970-01-01
      • 1970-01-01
      • 2021-06-23
      • 2023-03-14
      相关资源
      最近更新 更多