【问题标题】:How to pass just a component's function to another component without rendering it如何只将一个组件的功能传递给另一个组件而不渲染它
【发布时间】:2020-05-24 08:09:55
【问题描述】:

React 新手,我想让一个组件的按钮通过调用另一个组件中的函数来打开一个通用模式。最终,我希望导航栏有一个按钮,一个引导卡有一个按钮,当单击它时,它会在单独的组件中打开一个模式。我尝试使用 ref={} 但我不明白我希望他们是另一种方式。这是我希望 ClickButton.js 中的按钮打开 Header.js 中的模态的简单代码。当我带来 ClickButton 父级时,我必须添加它,它会添加两个按钮。如何仅获取 CLickButton.js 的按钮来访问 Headerjs 中的切换功能。不会导致两个按钮呈现如下所示?

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;

这是模态

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;

这是显示两个按钮的图像。我如何让它只显示一次 CLickButton 按钮。

【问题讨论】:

  • 我不确定我是否遵循。 ClickButton 呈现单个按钮。不相关:请考虑一致的、标准化的缩进和空格。这很难阅读。

标签: javascript reactjs function redux reactstrap


【解决方案1】:

要访问类方法中的道具,您可能需要将类方法显式绑定到实例。

  render( ) {
    return (
      <div>
        <Button onClick={this.onClicker.bind(this)} color="primary">OtherComponentButton</Button>
      </div>
    );
  }

或者你也可以直接调用prop方法

  render( ) {
    return (
      <div>
        <Button onClick={this.props.toggleModal} color="primary">OtherComponentButton</Button>
      </div>
    );
  }

【讨论】:

    【解决方案2】:

    不确定我是否理解这个问题,但是这个呢?

    点击按钮:

    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>;
      }
    }
    

    模态:

    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 组件定义 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>
        );
      }
    }
    

    【讨论】:

    • 我了解 app.js 以及父母和孩子。我将举一个更好的例子来说明我想要发生的事情。假设我有一个导航栏和一个页脚作为两个单独的组件,每个组件都有一个按钮来打开一个联系模式表单。我有第三个组件,即联系模式表单。所有都像往常一样列在 app.js 中。我希望按钮专门调用联系人模式表单组件中的切换功能来打开它。当我可以通过调用切换函数从其他组件打开一个通用模态组件时,我不想在每个组件中编写模态表单。
    猜你喜欢
    • 2020-05-27
    • 1970-01-01
    • 2021-02-16
    • 2017-12-19
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    相关资源
    最近更新 更多