Reactjs Redux失败的属性类型:无法将类作为函数调用
我设计了一个reduxapi来显示通知。它可以工作,但我在第一次执行时收到警告。这个:Reactjs Redux失败的属性类型:无法将类作为函数调用,reactjs,redux,react-redux,Reactjs,Redux,React Redux,我设计了一个reduxapi来显示通知。它可以工作,但我在第一次执行时收到警告。这个: Failed prop type: Cannot call a class as a function in AlertsContainer (created by Connect(AlertsContainer)) in Connect(AlertsContainer) (created by App) in App (created by Connect(App)) in
Failed prop type: Cannot call a class as a function
in AlertsContainer (created by Connect(AlertsContainer))
in Connect(AlertsContainer) (created by App)
in App (created by Connect(App))
in Connect(App) (created by Root)
in Provider (created by Root)
in Root
/*****容器***/
import React from 'react';
import {connect} from 'react-redux';
import PropTypes from "prop-types";
import {closeNotification} from './alertActions'
import Alerts from './Alerts';
import Alert from './alert';
import alertConfiguration from './alertConfiguration';
class AlertsContainer extends React.Component {
static propTypes = {
alerts:PropTypes.arrayOf(Alert).isRequired,
closeNotification:PropTypes.func.isRequired
};
handleClose = (alert) => {
this.props.closeNotification(alert);
};
render() {
const {alerts} = this.props;
return <Alerts alertConfig={alertConfiguration}
alerts={alerts}
onAlertDismissed={this.handleClose} />;
}
}
const mapStateToProps = (state, ownProps) => {
return {alerts:state.alerts};
};
export default connect(mapStateToProps,{closeNotification})(AlertsContainer);
我已按要求添加了操作
/*编辑*/
/****警报对象****/
export default class Alert {
constructor(message) {
this._id = new Date().getTime();
this.type = "danger";
this._headline="";
this.message = message;
}
get headline() {
return this._headline;
}
set headline(value) {
this._headline = value;
}
get id() {
return this._id;
}
get type() {
return this._type;
}
set type(value) {
this._type = value;
}
get message() {
return this._message;
}
set message(value) {
this._message = value;
}
}
我已按要求添加了警报对象。我认为问题出在
PropTypes.arrayOf(Alert).isRequired
中PropTypes.arrayOf
需要验证程序函数而不是类类型。请参阅文档。请添加alertActions
我刚刚添加了它们从导入警报。/Alert'
您也可以显示Alert.js吗?我已根据要求添加了Alert.js。我尝试将其更改为PropTypes.array.isRequired
,但没有更改。
import {
DISMISS_NOTIFICATION,
SHOW_NOTIFICATION
} from "./alertConstants";
export const showNotification = (notificationType,notificationProps) => (dispatch, getState) => {
const newAlert ={
id: notificationProps.id,
type:notificationProps.type,
headline:notificationProps.headline,
message: notificationProps.message
};
return dispatch({type : SHOW_NOTIFICATION, notificationType:notificationType,notificationProps:newAlert});
};
export const closeNotification = (notification) => (dispatch, getState) => {
return dispatch({type : DISMISS_NOTIFICATION, currentNotification : notification});
};
export default class Alert {
constructor(message) {
this._id = new Date().getTime();
this.type = "danger";
this._headline="";
this.message = message;
}
get headline() {
return this._headline;
}
set headline(value) {
this._headline = value;
}
get id() {
return this._id;
}
get type() {
return this._type;
}
set type(value) {
this._type = value;
}
get message() {
return this._message;
}
set message(value) {
this._message = value;
}
}