我有一个需要调用动作分派器的组件,但是当我触发它时,出现“未定义不是函数”错误。
我正在使用react-redux,redux-persist和redux-thunk。
我所有的其他动作在调用时都可以正常工作。
UserActions.js
export const updateProfile = (user, token) => {
return async dispatch => {
try {
axios()
.then(response => {
getUpdateProfileSuccess(response.data, dispatch);
})
.catch(error => {
getUpdateProfileError(error.response, dispatch);
});
} catch (error) {
getUpdateProfileError(error.response, dispatch);
}
};
};
const getUpdateProfileSuccess = (data, dispatch) => {
dispatch({
type: UPDATE_PROFILE_SUCCESS,
data
});
};
const getUpdateProfileError = (error, dispatch) => {
dispatch({
type: UPDATE_PROFILE_ERROR,
error
});
};
我的component.js
import...
import { updateProfile } from "../../../../Actions/UserActions";
class CardInfo extends Component {
_handleUpdateProfile = () => {
let user = {...};
this.props.updateProfile(user, this.props.token);
}
render() {
return (...)
}
}
const mapStateToProps = state => ({
token: state.UserReducer.token,
user_data: state.UserReducer.user_data
});
export default connect(
mapStateToProps,
{ updateProfile }
)(CardInfo);
当我调试代码时,出现此错误:
消息:“未定义updateProfile”
堆栈:“ ReferenceError:未定义updateProfile 在评估时(在CardInfo._this._handleUpdateProfile评估时(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:151091:21), :1:1) 在Object.CardInfo._this._handleUpdateProfile上[按印刷时](blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:151091:21) at Object.Basic._this._handlePress [as onPress](blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:124999:21) 在Object.touchableHandlePress(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:46460:40) 在Object._performSideEffectsForTransition(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:45131:16) 在Object._receiveSignal处(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:45060:14) 在Object.touchableHandleResponderRelease(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:44939:12) 在Object.invokeGuardedCallbackImpl(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:7899:16) 在invokeGuardedCallback(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:7990:37)处 在invokeGuardedCallbackAndCatchFirstError(blob:http://localhost:8081/41e77cd1-d0ff-413b-a32f-dfca9ee6061e:7994:31)“
答案 0 :(得分:-1)
AFAIK,您应该将props
添加为构造函数的参数。
class CardInfo extends Component {
constructor(props) {
super(props);
}
...
}