How to call a mapStateToProps path with numerous dynamic variables (React-Redux)

367 Views Asked by At

Not sure exactly what the correct syntax for this is, but I am trying to get the value (string) of the courseTitle variable found in state as the following path (when typing console.log(state):

state
|_____ firestore
|______________ data
|_____________________ "userID"
|_____________________________ "courseID"
|________________________________________ courseTitle

the "userID" and "courseID" are dynamic variables whose value changes depending on the user that is signed in and the course that user selected. courseTitle is a variable that contains the title of the course, and is what is being read in via mapStateToProps.

1

There are 1 best solutions below

0
On BEST ANSWER

NOTE: SOLUTION
While writing the question, I was able to test some things out and ultimately find a solution. However, I thought sharing my code here might help future coders who experience a similar problem.

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,

    }
}