javascript - 如何在其他组件中使用组件方法
问题描述
我创建了一个通知组件。我想在其他组件中使用它
如何this.addNotification()
在我的 navbar.js 中使用?我尝试了很多方法,但都没有奏效,所以也许这里有人知道最好的方法。我会很高兴得到任何帮助!
//Notifications.js
import React from "react";
import ReactNotification from "react-notifications-component";
import "react-notifications-component/dist/theme.css";
export default class Notifications extends React.Component {
constructor(props) {
super(props);
this.addNotification = this.addNotification.bind(this);
this.notificationDOMRef = React.createRef();
}
addNotification() {
this.notificationDOMRef.current.addNotification({
title: "Awesomeness",
message: "Awesome Notifications!",
type: "success",
insert: "top",
container: "top-right",
animationIn: ["animated", "fadeIn"],
animationOut: ["animated", "fadeOut"],
dismiss: { duration: 2000 },
dismissable: { click: true }
});
}
render() {
return (
<div className="app-content">
<ReactNotification ref={this.notificationDOMRef} />
<button ref={this.addNotification} className="btn btn-primary">
Add Awesome Notification
</button>
</div>
);
}
}
//Navbar.js
// I want to use the notifications here, but i don't know how
// this.addNotification() - > this is working in the notifications file, `but not here`
import React from 'react';
import {
Collapse,
Navbar,
NavbarToggler,
NavbarBrand,
Nav,
NavItem,
NavLink,
UncontrolledDropdown,
Dropdown,
DropdownToggle,
DropdownMenu,
DropdownItem, Container, Row, Col } from 'reactstrap';
import LoginModal from './LoginModal';
import User from './User';
// i just import the class
import Notifications from './Notifications';
export default class MyNavbar extends User {
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.toggleDropDown = this.toggleDropDown.bind(this);
this.state = {
isOpen: false,
userName: this.getUserLogin(),
userEmail: this.getUserEmail(),
firstTime: this.getFirstTime(),
dropdownOpen: false,
info:""
};
}
showError(){
this.addNotification()
// I want something like this, but i don't know how to do this
}
toggle() {
this.setState({
isOpen: !this.state.isOpen
});
}
toggleDropDown() {
this.setState({
dropdownOpen: !this.state.dropdownOpen
});
}
render(props) {
return (
<div>
<Container>
<Navbar color="da" light expand="md">
<NavbarBrand href="/">Pies Fajny Jest</NavbarBrand>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
{this.state.userName ?
<Nav className="ml-auto" navbar>
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggleDropDown}>
<DropdownToggle className="btn btn-danger" caret>
{this.state.userName}
</DropdownToggle>
<DropdownMenu>
<DropdownItem header>Header</DropdownItem>
<DropdownItem>Some Action</DropdownItem>
<DropdownItem disabled>Action (disabled)</DropdownItem>
<DropdownItem divider />
<DropdownItem>Foo Action</DropdownItem>
<DropdownItem>Bar Action</DropdownItem>
<DropdownItem onClick={ () => this.logout(this) }>Wyloguj</DropdownItem>
</DropdownMenu>
</Dropdown>
</Nav>
:
<Nav className="ml-auto" navbar>
<NavItem className="LoginRegister" >
<LoginModal
buttonLabel="Zaloguj się"
title="Logowanie"
inputSubmit="Zaloguj się"
/>
</NavItem>
<NavItem className="LoginRegister" >
<LoginModal
buttonLabel="Zarejestruj się"
title="Rejestracja"
inputSubmit="Zarejestruj się"
register="register"
/>
</NavItem>
</Nav>}
</Collapse>
</Navbar>
</Container>
</div>
);
}
}
解决方案
您可以将方法作为道具传递,例如
<Notifications addNotification={this.addNotification} />
当您控制台 this.props.addNotification 时,您会在 Notifications.js 中获得该功能
推荐阅读
- python - python numpy创建逻辑数组
- react-native - React Native snapToOffsets 只为一个快照,然后正常滚动
- javascript - Rxjs 在管道中调用另一个订阅并以角度更改根订阅的变量
- single-page-application - Auth0 - 用户保持登录状态多长时间?
- python - 如何使用一个数据框的内容来索引另一个多级索引数据框?
- postgresql - Keycloak 在启动期间重试数据库连接
- python - 将函数应用于数据框列时处理空值
- c++ - 是否可以将 Qt WebEngine 中的代理 URL 列入白名单
- regex - 使用正则表达式在bash中提取字符串的第一个单词
- bash - 在bash中搜索多个目录中的文件