简体   繁体   English

如何让一个按钮点击 React 中不同组件的另一个按钮

[英]How to make a button click another button from different components in React

I'm new to react and it is kinda hard to understand the one way data flow on it, i was making a simple app and i'm using mdbootstrap for some ready bootstrap components, I imported the component of a modal (which has a button when clicked it toggles a modal) so in my app i have cards, each one has a button that's supposed to toggle the button, but i couldn't figure out how to link the card's button with the mdbootstrap component's button.我是新手,很难理解数据流的单向方式,我正在制作一个简单的应用程序,我正在使用 mdbootstrap 作为一些现成的引导程序组件,我导入了一个模态的组件(它有一个单击它会切换模式)所以在我的应用程序中,我有卡片,每个卡片都有一个按钮,应该可以切换按钮,但我不知道如何将卡片的按钮与 mdbootstrap 组件的按钮链接起来。

The Card component:卡片组件:

import React, { Component } from 'react';
import ModalPage from './modal.jsx'


class Card extends Component {

    render() {
        return (
            <div>
                <div className="card m-5" style={{ width: '18rem' }}>
                    <img src={this.props.img} className="card-img-top" />
                    <div className="card-body">
                        <h5 className="card-title">{this.props.title}</h5>
                        <p className="card-text">{this.props.desc}</p>
                        <button onClick={/*I don't know what exactly i should put here */}></button>
                    </div>
                </div>
            </div>
        )
    }
}

export default Card;

The modal componant:模态分量:

import React, { Component } from 'react';
import { MDBContainer, MDBBtn, MDBModal, MDBModalBody, MDBModalHeader, MDBModalFooter } from 'mdbreact';

class ModalPage extends Component {
    state = {
        modal13: false
    }

    toggle = nr => () => {
        let modalNumber = 'modal' + nr
        this.setState({
            [modalNumber]: !this.state[modalNumber]
        });
    }

    render() {
        return (

                <MDBContainer>

                    {/* This is the button I want to click when clicking the card's button */}
                    <MDBBtn color="primary" onClick={this.toggle(13)}>
                        Modal
                    </MDBBtn>


                    <MDBModal isOpen={this.state.modal13} toggle={this.toggle(13)}>
                        <MDBModalHeader toggle={this.toggle(13)}>
                            {this.props.title}
                        </MDBModalHeader>
                        <MDBModalBody>
                            {/* edit here */}
                            {this.props.content}
                        </MDBModalBody>
                        <MDBModalFooter>
                            <MDBBtn color="secondary" onClick={this.toggle(13)}>
                                Close
                            </MDBBtn>
                            <MDBBtn color="primary">Save changes</MDBBtn>
                        </MDBModalFooter>
                    </MDBModal>
                </MDBContainer>


        );
    }
}

export default ModalPage;

Rather than having 2 click events you only need one on the child component.而不是有 2 个点击事件,您只需要在子组件上有一个。 Instead of trying to send a click to the parent button in order to call toggle() just pass the toggle function to the child to be called:与其尝试向父按钮发送点击以调用 toggle(),只需将 toggle 函数传递给要调用的子级:

Card:卡片:

import React, { Component } from 'react';
import ModalPage from './modal.jsx'


class Card extends Component {

    render() {
        return (
            <div>
                <div className="card m-5" style={{ width: '18rem' }}>
                    <img src={this.props.img} className="card-img-top" />
                    <div className="card-body">
                        <h5 className="card-title">{this.props.title}</h5>
                        <p className="card-text">{this.props.desc}</p>
                      //*****************************************
                        <button onClick={this.props.click}></button>
                      //*****************************************
                    </div>
                </div>
            </div>
        )
    }
}

export default Card;

Modal:模态:

import React, { Component } from 'react';
import { MDBContainer, MDBBtn, MDBModal, MDBModalBody, MDBModalHeader, MDBModalFooter } from 'mdbreact';

class ModalPage extends Component {
    state = {
        modal13: false
    }

    toggle = nr => () => {
        let modalNumber = 'modal' + nr
        this.setState({
            [modalNumber]: !this.state[modalNumber]
        });
    }

    render() {
        return (

                <MDBContainer>

                    {/* I am assuming that this is a reference to <Card /> - simply pass in the onClick function as a parameter.  You can even use onClick here and this.props.onClick in the child element */}
                    <MDBBtn color="primary" click={this.toggle(13)}>
                        Modal
                    </MDBBtn>


                    <MDBModal isOpen={this.state.modal13} toggle={this.toggle(13)}>
                        <MDBModalHeader toggle={this.toggle(13)}>
                            {this.props.title}
                        </MDBModalHeader>
                        <MDBModalBody>
                            {/* edit here */}
                            {this.props.content}
                        </MDBModalBody>
                        <MDBModalFooter>
                            <MDBBtn color="secondary" onClick={this.toggle(13)}>
                                Close
                            </MDBBtn>
                            <MDBBtn color="primary">Save changes</MDBBtn>
                        </MDBModalFooter>
                    </MDBModal>
                </MDBContainer>


        );
    }
}

export default ModalPage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM