【发布时间】:2017-07-14 06:10:45
【问题描述】:
我正在使用 ReactJS 和 React-Router 构建一个 SPA。应用程序是我的主要组件,其他一切都源于此。在该组件上,我添加了一个 ToastContainer 并且它在该组件上运行良好。我已经将该函数传递给子组件,希望它们可以调用并显示消息。当我尝试这样做时,我得到了
Uncaught TypeError: Cannot read property 'toastContainer' of undefined
应用程序/主要组件
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, IndexRoute, browserHistory, hashHistory } from 'react-router';
import ParameterContainer from './components/parameter/parameter-container';
import ParameterValueContainer from './components/parameter/parameter-value-container';
import NavMenu from './components/navigation/nav-menu';
import {Alert} from 'react-bootstrap';
import ReactToastr from 'react-toastr';
import {ToastContainer, ToastMessage} from 'react-toastr';
let ToastMessageFactory = React.createFactory(ToastMessage.animation);
// Main component and root component
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
userId: null,
roles: null,
parameterTypes: {
'STRING': 'STRING',
'BOOLEAN': 'BOOLEAN',
'INTEGER': 'INTEGER',
'DECIMAL': 'DECIMAL'
},
parameterGroups: {
1: 'POS',
2: 'MenuStructure'
}
};
}
componentDidMount() {
//this.addAlert('Success', 'Parameter Created');
this.addErrorAlert('Ohhhh snap!', 'You messed up Rodney, you messed up bad!');
}
addAlert(title, message) {
this.refs.toastContainer.success(
title,
message,
{
timeOut: 10000,
extendedTimeOut: 10000,
preventDuplicates: true,
positionClass: "toast-bottom-full-width",
showMethod: "fadeIn",
hideMethod: "fadeOut"
}
);
}
addErrorAlert(title, message) {
this.refs.toastContainer.error(
message,
title,
{
timeOut: 10000,
extendedTimeOut: 10000,
preventDuplicates: true,
positionClass: "toast-bottom-full-width",
showMethod: "fadeIn",
hideMethod: "fadeOut"
}
);
}
render() {
return (
<div>
<NavMenu />
<div className="container-fluid">
{React.Children.map(
this.props.children,
child => React.cloneElement(child,
{
parentState: this.state,
path: this.props.route.path,
alertCallback: this.addErrorAlert
})
)}
<ToastContainer ref="toastContainer" toastMessageFactory={ToastMessageFactory} className="toast-bottom-full-width">
</ToastContainer>
</div>
</div>
)
}
}
// page for 404
class NoMatch extends React.Component {
render() {
return (
<div className="container">
<Alert bsStyle="danger">
<h1>404: Not Found</h1>
<h3>The requested resource does not exist!</h3>
</Alert>
<img src="images/404.png" style={{display: 'block', margin: '0 auto', width: 300, height: '*'}} />
</div>
)
}
}
// render the application
ReactDOM.render((
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="parameter" component={ParameterContainer} />
<Route path="parametervalues" component={ParameterValueContainer} />
<Route path="*" component={NoMatch}/>
</Route>
</Router>
), document.getElementById('react'))
我在子组件中使用这样的回调
componentDidMount() {
this.props.alertCallback("OHHHH NOOOO!!!!", "Something has gone wrong!");
this.fetchFromApi();
}
【问题讨论】:
-
所以,只是一个简单的问题,如果您从 App/Container 调用 addErrorAlert(),烤面包机是否工作?我的意思是你是从 componentDidMount 调用它,这行得通吗?
-
是的,该组件可以正常工作
-
你使用的是什么版本的 react-toastr?你为什么不使用 ToastMessageAnimated 代替?文档似乎已过时,提供的示例不再有效...
标签: javascript reactjs ecmascript-6 react-router