简体   繁体   English

如何仅将组件的 function 传递给另一个组件而不渲染它

[英]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.

相关问题 如何在不渲染组件的情况下传递道具? - How to pass props without rendering the component? 如何在另一个函数组件的 scope 中将变量作为道具传递 - How to pass a variable as props inside the scope of another function's component 如何在不重新渲染组件的情况下更改 useState 的状态,而只是重新渲染其组件 - How to change useState's state without re-rendering the component, but just its components 如何将函数传递给另一个组件进行反应 - How to pass a function into another component in react 如何将一个函数传递给另一个React组件? - How to pass a function to another react component? 如何在不渲染子组件的情况下在 React 组件之间传递数据 - How to pass data between React components without rendering the child component 如何从另一个组件的功能更新一个组件的数组? - How to update array of a component from another component's function? 如何在不使用 Arrow Function 的情况下将方法从一个组件传递到另一个组件? - How to pass method from one component to another without using Arrow Function? 如何将一个反应组件传递给另一个反应组件以包含第一个组件的内容? - How to pass in a react component into another react component to transclude the first component's content? React 在 onChange 函数中如何更新另一个自定义组件的状态并传入一个值? - In React when in onChange function how to update another custom component's state and pass a value in?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM