I am storing data for various rooms
in a global app container. I have a sub-container called roomDetails
that uses a reselect selector to select a room from the global state using ownProps.params.slug
. This is done using mapStateToProps.
My problem is that when I change route params (rooms/:roomId
) from:
rooms/roomid1
to
rooms/roomid2
the component room props are not updating.
I'm changing the route using Link:
<Link to={"/room/roomid1"} key={"room-roomid1"}>room 1</Link>
How can I get the component's room
props to reselect a room from the global rooms state when the route params change?
Note, the room props load as they should on first page load, or if i go to a route that's outside of the dynamic route, and then return. They just don't load correctly when switching between the same dynamic route, where only component properties (route params) are changing.
RoomDetails/index.js
const mapStateToProps = (state, ownProps) => {
return createStructuredSelector({
room: selectRoom(ownProps.params.roomId),
});
}
...
render() {
let roomContent = null;
if (this.props.room) {
roomContent = (
<div>
{this.props.room.get('name')}
</div>
);
}
return (
<div>
{roomContent}
</div>
);
}
selectors.js
import { createSelector } from 'reselect';
const selectGlobal = () => (state) => state.get('global');
const selectRooms = () => createSelector(
selectGlobal(),
(globalState) => globalState.get('rooms')
);
const selectRoom = (roomId) => createSelector(
selectRooms(),
(roomsState) => roomsState && roomsState.get(roomId)
);