我觉得这里缺少一些简单的东西,但是无论出于什么原因,这个简单的ErrorMessageAlert都不会分派动作。我已经分派了很多次,但似乎无法弄清这个错误。
谁能再借一双眼睛?
这是我的组件
import { connect } from 'react-redux';
import styled from 'react-emotion';
import { resetErrorMessage } from 'users/ducks'
const ErrorMessage = styled.div`
width: 100%;
color: red;
position: fixed;
background: #F9DADA;
text-align: center;
border-bottom: 1px solid red;
padding-top: 5px;
padding-bottom: 5px;
z-index: 1;
`
export class ErrorMessageAlert extends Component<props> {
state = { isHidden: true }
componentDidMount() {
console.log("triggered")
// debugger
const { resetErrorMessageAction } = this.props
resetErrorMessageAction()
}
render(){
const {
errorMessage,
} = this.props
console.log(this.state.isHidden)
return (
<div>
{
this.state.isHidden && <ErrorMessage>{errorMessage}</ErrorMessage>
}
</div>
)
}
}
const mapDispatchToProps = (dispatch) => ({
resetErrorMessageAction: () => dispatch(resetErrorMessage()),
})
export default connect(null, mapDispatchToProps)(ErrorMessageAlert);
在用户/鸭子中
export const RESET_ERROR_MESSAGE = 'RESET_ERROR_MESSAGE';
export const resetErrorMessage = () => ({
type: RESET_ERROR_MESSAGE,
})
这是主要的错误消息堆栈跟踪:
ErrorMessageAlert.js:29 Uncaught TypeError: resetErrorMessageAction is not a function
at ErrorMessageAlert.componentDidMount (ErrorMessageAlert.js:29)
at ErrorMessageAlert.componentDidMount (react-hot-loader.development.js:654)
at commitLifeCycles (react-dom.development.js:17334)
at commitAllLifeCycles (react-dom.development.js:18736)
at HTMLUnknownElement.callCallback (react-dom.development.js:149)
at Object.invokeGuardedCallbackDev (react-dom.development.js:199)
at invokeGuardedCallback (react-dom.development.js:256)
at commitRoot (react-dom.development.js:18948)
at react-dom.development.js:20418
at Object.unstable_runWithPriority (scheduler.development.js:255)
```
答案 0 :(得分:0)
resetErrorMessageAction
是undefined
,因为自从使用导出默认功能以来,我已从其他文件中错误地导入了该文件。
所以您需要正确导入文件。
import { ErrorMessageAlert } from 'error/ErrorMessageAlert';
vs
import ErrorMessageAlert from 'error/ErrorMessageAlert';