admin管理员组文章数量:1356840
I'm now implementing Rails like flash messaging to my React app. The flash message itself is fine but now I want the flash message to disappear automatically in some pages. I at first used setTimeout in my Flash ponent but received this error.
Uncaught Error: Invariant Violation: enqueueCallback(...): You called setProps, replaceProps, setState, replaceState, or forceUpdate with a callback that isn't callable.
Here is the code.
import React from 'react/addons';
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;
class Flash extends React.Component {
constructor(props) {
super(props);
this.state = {
message: this.props.message
}
}
ponentWillReceiveProps(nextProps) {
let _this = this;
this.setState({
message: nextProps.message
});
if (nextProps.autoDisappear) {
window.setTimeout(() => {
_this.setState({
message: null
}, 2000)
})
}
}
onClick(e) {
this.setState({
message: null
});
}
render() {
let transitionName = "flash-anim"
if (this.state.message) {
return (
<ReactCSSTransitionGroup transitionAppear={true} transitionName={transitionName} transitionEnterTimeout={200} transitionLeaveTimeout={300}>
<div className="flash-container" id="flash-ponent">
<div className="alert">
<a className="close alert-close" onClick={this.onClick.bind(this)}>x</a>
{this.state.message}
</div>
</div>
</ReactCSSTransitionGroup>
);
} else {
return <ReactCSSTransitionGroup transitionAppear={true} transitionName={transitionName} transitionEnterTimeout={200} transitionLeaveTimeout={300} />;
}
}
}
export default Flash;
I consider other way to resolve this, but so far no idea. Do you have any idea to resolve this issue?
I'm now implementing Rails like flash messaging to my React app. The flash message itself is fine but now I want the flash message to disappear automatically in some pages. I at first used setTimeout in my Flash ponent but received this error.
Uncaught Error: Invariant Violation: enqueueCallback(...): You called setProps, replaceProps, setState, replaceState, or forceUpdate with a callback that isn't callable.
Here is the code.
import React from 'react/addons';
var ReactCSSTransitionGroup = React.addons.CSSTransitionGroup;
class Flash extends React.Component {
constructor(props) {
super(props);
this.state = {
message: this.props.message
}
}
ponentWillReceiveProps(nextProps) {
let _this = this;
this.setState({
message: nextProps.message
});
if (nextProps.autoDisappear) {
window.setTimeout(() => {
_this.setState({
message: null
}, 2000)
})
}
}
onClick(e) {
this.setState({
message: null
});
}
render() {
let transitionName = "flash-anim"
if (this.state.message) {
return (
<ReactCSSTransitionGroup transitionAppear={true} transitionName={transitionName} transitionEnterTimeout={200} transitionLeaveTimeout={300}>
<div className="flash-container" id="flash-ponent">
<div className="alert">
<a className="close alert-close" onClick={this.onClick.bind(this)}>x</a>
{this.state.message}
</div>
</div>
</ReactCSSTransitionGroup>
);
} else {
return <ReactCSSTransitionGroup transitionAppear={true} transitionName={transitionName} transitionEnterTimeout={200} transitionLeaveTimeout={300} />;
}
}
}
export default Flash;
I consider other way to resolve this, but so far no idea. Do you have any idea to resolve this issue?
Share Improve this question edited Nov 18, 2015 at 16:06 akmozo 9,8393 gold badges29 silver badges44 bronze badges asked Nov 18, 2015 at 11:17 Atsuhiro TeshimaAtsuhiro Teshima 1,5181 gold badge12 silver badges21 bronze badges2 Answers
Reset to default 5It seems that you are trying to pass 2000 as second parameter to this.setState
instead of setTimeout
. It should be:
window.setTimeout(() => {
this.setState({
message: null
});
}, 2000);
And as you already used arrow function
so no need to use _this. Just use this
.
You're trying to pass the integer 2000
as a callback to this.setState
.
window.setTimeout(() => {
_this.setState({
message: null
}, 2000)
});
You most likely want to pass that to the timeout function instead.
window.setTimeout(() => {
_this.setState({message: null});
}, 2000);
Also by using the arrow notation you're effectively binding this
to your ponent anyways, so you can skip saving a reference to this
.
本文标签: javascriptReact Make flash message disappear automaticallyStack Overflow
版权声明:本文标题:javascript - React: Make flash message disappear automatically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744057877a2583583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论