[英]How to pass just a component's function to another component without rendering it
New to React and I want to have a component's button open a general modal by calling a function in another component. React 新手,我想通过在另一个组件中调用 function 来让组件的按钮打开一个通用模式。 Eventually, I want a navbar to have a button, a bootstrap card to have a button, when clicked it opens one modal in a separate component.最终,我希望导航栏有一个按钮,一个引导卡有一个按钮,当点击它时,它会在单独的组件中打开一个模式。 I tried with ref={} but I don't understand it I hope their is another way.我尝试使用 ref={} 但我不明白我希望他们是另一种方式。 Here is my simple code in which I want that button in ClickButton.js to open the Modal in Header.js.这是我的简单代码,我希望 ClickButton.js 中的按钮打开 Header.js 中的模态。 When I bring over the ClickButton parent, I have to add it, which add two buttons.当我带来 ClickButton 父级时,我必须添加它,即添加两个按钮。 How do I get just the CLickButton.js's button to access the toggle function in Headerjs.如何仅获取 CLickButton.js 的按钮来访问 Headerjs 中的切换 function。 without causing two buttons to be rendered as shown below?不会导致两个按钮呈现如下所示?
import React, { Component } from 'react';
import {Button} from 'reactstrap';
import Header from './Header';
class ClickButton extends Component {
constructor(props) {
super(props);
this.state = {
};
}
onClicker = (props) => {
this.props.toggleModal();
}
render( ) {
return (
<div>
<Button onClick={this.onClicker} color="primary">OtherComponentButton</Button>
</div>
);
}
}
export default ClickButton;
Here is the Modal这是模态
import React, { Component } from "react";
import { Button, Modal, ModalBody, Form, FormGroup,Label,Input} from "reactstrap";
import ClickButton from './ClickButton';
class Header extends Component {
constructor(props) {
super(props);
this.state = {
modal: false
};
}
toggle = () => {
this.setState({
modal: !this.state.modal
});
}
render() {
return (
<div>
<Modal isOpen={this.state.modal}>
<ModalBody>
<Form>
<FormGroup>
<Label for="exampleEmail">Name</Label>
<Input type="email" name="email" id="exampleEmail" placeholder="Enter Name" />
</FormGroup>
</Form>
</ModalBody>
<Button onClick={this.toggle}>Close</Button>
</Modal>
<Button onClick={this.toggle}>ModalComponent</Button>
<ClickButton toggleModal={this.toggle} />
</div>
);
}
}
export default Header;
Here is the image that shows both buttons.这是显示两个按钮的图像。 How do I get it to just show the CLickButton button only once.我如何让它只显示一次 CLickButton 按钮。
To access props in a class method you may need to explicitly bind the class method to the instance.要在 class 方法中访问道具,您可能需要将 class 方法显式绑定到实例。
render( ) {
return (
<div>
<Button onClick={this.onClicker.bind(this)} color="primary">OtherComponentButton</Button>
</div>
);
}
Or you can also just call the prop method directly或者你也可以直接调用 prop 方法
render( ) {
return (
<div>
<Button onClick={this.props.toggleModal} color="primary">OtherComponentButton</Button>
</div>
);
}
Not sure that I understand the issue, but what about this?不确定我是否理解这个问题,但是这个呢?
ClickButton:点击按钮:
class ClickButton extends Component {
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.props.handleClick();
}
render() {
return <button onClick={this.handleClick}>OtherComponentButton</button>;
}
}
Modal:模态:
class Header extends Component {
constructor(props) {
super(props);
this.onClick = this.onClick.bind(this);
}
onClick() {
this.props.handleClick();
}
render() {
return (
<>
<Modal isOpen={this.props.modal}>
<ModalBody>
<Form>
<FormGroup>
<Label for="exampleEmail">Name</Label>
<Input
type="email"
name="email"
id="exampleEmail"
placeholder="Enter Name"
/>
</FormGroup>
</Form>
</ModalBody>
<Button onClick={this.onClick}>Close</Button>
</Modal>
<Button onClick={this.onClick}>ModalComponent</Button>
</>
);
}
}
App component defines state.modal property and provides it via props to Modal, also defines handleToggle used in ClickButton and Modal to manage state.modal: App 组件定义了 state.modal 属性并通过 props 提供给 Modal,还定义了在 ClickButton 和 Modal 中使用的 handleToggle 来管理 state.modal:
class App extends Component {
constructor(props) {
super(props);
this.state = {
modal: false,
};
this.handleToggle = this.handleToggle.bind(this);
}
handleToggle() {
this.setState({
modal: !this.state.modal,
});
}
render() {
return (
<div>
<Header modal={this.state.modal} handleClick={this.handleToggle} />
<ClickButton handleClick={this.handleToggle} />
</div>
);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.