【发布时间】:2020-06-09 13:36:33
【问题描述】:
我试图用 mapDispatchToProps 将一个 redux 操作(setMarker)分派到一个组件中,但是每当我调用该操作时,我都会得到这个
TypeError: setMarker 不是函数
import React from 'react';
import { connect } from 'react-redux';
import { setMarker } from '../../redux/map/map.actions';
import './suggestion.styles.scss';
const Suggestion = (location, { setMarker }) => {
const { area, city, address } = location;
return (
<div className="suggestion" onClick={() => console.log(setMarker)}>
<i className="fas fa-map-marker-alt fa-2x"></i>
<div className="address">
<h3 className="location-title">{`${area}, ${city}`}</h3>
<p className="location-desc">{address}</p>
</div>
</div>
);
};
const mapDispatchToProps = {
setMarker,
};
export default connect(null, mapDispatchToProps)(Suggestion);
我的动作文件包含以下代码
import mapActionTypes from './map.types';
export const setMarker = (location) => ({
type: mapActionTypes.SET_MARKER,
payload: location,
});
我的reducer文件包含以下代码
import mapActionTypes from './map.types';
const INITIAL_STATE = {
location: {
lat: 23.810331,
lng: 90.412521,
},
};
const mapReducer = (state = INITIAL_STATE, action) => {
switch (action.type) {
case mapActionTypes.SET_MARKER:
return {
...state,
location: action.payload,
};
default:
return state;
}
};
export default mapReducer;
【问题讨论】:
标签: reactjs redux react-redux