新的反应,不太确定什么是最好的方法在这里。
我想要做的是当我在另一个组件上运行事件时呈现一个组件(一个错误消息组件)。
这里的问题是,我也应该能够呈现多个通知,每个通知都有不同的状态。(错误、警告、成功)。
其他信息,
我的主应用程序类,
MainLayout = React.createClass({
render() {
return (
<div>
{this.props.header}
{this.props.notification}
{this.props.content}
{this.props.footer}
</div>
)
}
});这是我的通知类,它在{this.props.notification}中呈现
SwiftNotification = React.createClass({
getInitialState() {
return {
type: 'not-error',
}
},
render() {
return (
<div className={ this.state.type + " jwlnotification container-fluid text-center"}>
text for this....
</div>
)
}
});所以在inviteMembers函数中,我希望能够呈现通知组件,但是在这个组件之外。
BandEvent = React.createClass({
propTypes: {
id: React.PropTypes.string.isRequired,
name: React.PropTypes.string.isRequired,
location: React.PropTypes.string.isRequired
},
inviteMembers(e) {
e.preventDefault();
Meteor.call('inviteMembers', this.props.id, function(error, data) {
if (error) {
console.log(error.reason);
} else {
// what do we want to do here if we are successful..
console.log(data);
}
});
},
render() {
let { id, name, location } = this.props;
return (
<li className="bandEvent" ><button className="btn btn-info btn-sm" onClick={this.inviteMembers}> Invite Members </button> {name} at {location}</li>
)
}
});谢谢!
发布于 2016-01-31 04:10:15
您可以使用类似flux的实现,如redux,将通知信息存储在单个对象中。然后,任何组件都可以发送包含您需要的元数据(message、message_type)的通知。然后,您的通知组件将使用通知,并包含通知所需的所有业务逻辑:一次显示多少通知,如何显示通知,何时取消通知,等等。
编辑:看起来这是一个你可以使用的包,或者至少源代码是我描述的逻辑的一个例子。https://github.com/indexiatech/re-notif
发布于 2018-07-25 15:15:13
另一个变体是如何组织消息组件并从外部运行它的方法。
看看Confirm.openModal方法,任何React组件都可以调用它,而不管Confirm组件和call组件之间的关系。此外,它不需要在全局窗口中声明就可以工作。
https://stackoverflow.com/questions/35106368
复制相似问题