Reputation: 49
I am trying to update a map component in my app. I receive the location details from redux and I can see the state change in the Redux Dev Tools or even on my browser console but the map component does not seem to update unless I go to another route then return to the route containing the component.
I have read about Redux needing state to be immutable and I have tried all the suggested options to try to keep my state immutable but I still have not been successful.
const initialState = {
gps: [],
};
export default function locationsReducer(state = initialState, action) {
switch(action.type) {
case FETCH_LOCATIONS:
return {
...state,
gps: action.locations,
};
default:
return state;
}
}
class MapResults extends Component {
static defaultProps = {
center: {
lat: 0.0236,
lng: 37.9062
},
zoom: 6.5
};
render() {
return (
<div style={{ height: '70vh', width: '100%' }}>
<GoogleMapReact
bootstrapURLKeys={ key: process.env.GOOGLE_MAPS_KEY }}
defaultCenter={this.props.center}
defaultZoom={this.props.zoom}
>
{this.props.coordinates.gps.map((location, index) => {
return(
<Tooltip
lat={location.latitude}
lng={location.longitude}
title={location.address_name}
key={index}>
<Avatar src="/images/gps.svg" />
</Tooltip>
)
})}
</GoogleMapReact>
</div>
)
}
}
const mapStateToProps = state => {
return { coordinates: state.locations };
};
export default connect(mapStateToProps)(MapResults);
The fetchLocation method is being triggered correctly and coordinates being fetched and can be viewed in the dev tools. I just have to navigate to a different route then come back to the map route for the markers to be updated on the map.
I expect the map component to update immediately the redux state changes when the location state has been updated.
Any help will be much appreciated. Thanks!
My index reducer where I am combining my reducers
import { combineReducers } from "redux";
import individual from "./individualReducer";
import locations from "./locationsReducer";
export default combineReducers({
individual,
locations
});
Redux Dev Tools clearly shows state is updated as you can see in image
Map after redux state is updated
You can see no markers
Map after I navigate to another route and back to map route
You can see markers are updated
Upvotes: 1
Views: 2642
Reputation: 437
In your Location reducer, the state name is gps
, so the state name should be mapped in the component. Because the state gps
is updated. Please change the state name as to below
const mapStateToProps = state => {
return { coordinates: state.gps };
};
Upvotes: 2