【问题标题】:Conditonal rendering REACT modal component条件渲染 REACT 模态组件
【发布时间】:2020-05-31 16:38:00
【问题描述】:

我有一个视图模式组件 (ViewModal.js)

import React from 'react'
import { Button, Modal } from 'react-bootstrap';
import './Modal.css';

class ViewModal extends React.Component {

    constructor() {
        super();
        this.state = {
        }
    }

    handleModalShowHide() {
        this.setState({ showHide: !this.state.showHide })
    }

    render() {
        return (
            <div>
                <Button variant="success" onClick={() => this.handleModalShowHide()}>
                    Write a review
                </Button>

                <Modal show={this.state.showHide}>
                    <Modal.Header closeButton onClick={() => this.handleModalShowHide()}>
                        <Modal.Title>Add your review</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                        ModalBody
                    </Modal.Body>
                    <Modal.Footer>
                        <Button variant="outline-secondary" onClick={() => this.handleModalShowHide()}>
                            Close
                    </Button>
                        <Button variant="outline-success" onClick={() => this.handleModalShowHide()}>
                            Save Review
                    </Button>
                    </Modal.Footer>
                </Modal>

            </div>
        )
    }

}

export default ViewModal;

我将它导入另一个名为 viewcard.js 的功能组件中 viewcard.js的逻辑如下


import React from 'react';
import ViewModal from './ViewModal';
import Card from 'Card';

function handleClick(){
    console.log('in handle');
 }
const viewcard = () => {
    return ( 
        <p onClick={() => handleClick()}/>
         Some text in paragraph
        </p>
     );
}

export default viewcard;

卡片组件在屏幕上显示一些文本。 我想要实现的是当用户单击该文本时,我想显示模态。

目前如果我在视图卡中渲染模态,通过调用它,它将根据这行逻辑显示一个按钮

                <Button variant="success" onClick={() => this.handleModalShowHide()}>
                    Write a review
                </Button>

我想删除按钮并在用户单击 viewcard.js 中的文本时发生相同的行为

【问题讨论】:

  • 请在您的问题中添加Card 代码..
  • @HagaiHarari - 卡片只是一个占位符,我现在已将其更改为段落。

标签: javascript reactjs conditional-statements


【解决方案1】:

ViewCard 组件:-

import React, {useState} from 'react';
import ViewModal from './ViewModal';
import Card from 'Card';

const ViewCard = () => {
    const [showModal, setShowModal] = useState(false);

    function handleClick(){
       setShowModal(!showModal)
    }

    return ( 
        <Card onClick={() => handleClick()}/>
        {showModal && <ViewModal hideBtn={true} showModal={true} />}
     );
}

export default ViewCard;

ViewModal 组件:

import React from 'react'
import { Button, Modal } from 'react-bootstrap';
import './Modal.css';

class ViewModal extends React.Component {

    constructor() {
        super();
        this.state = {
           showHide: this.props.showModal ? true : false
        }
    }

    handleModalShowHide() {
        this.setState({ showHide: !this.state.showHide })
    }

    render() {
        return (
            <div>
                {this.props.hideBtn ? null : <Button variant="success" onClick={() => this.handleModalShowHide()}>
                    Write a review
                </Button>}

                <Modal show={this.state.showHide}>
                    <Modal.Header closeButton onClick={() => this.handleModalShowHide()}>
                        <Modal.Title>Add your review</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                        ModalBody
                    </Modal.Body>
                    <Modal.Footer>
                        <Button variant="outline-secondary" onClick={() => this.handleModalShowHide()}>
                            Close
                    </Button>
                        <Button variant="outline-success" onClick={() => this.handleModalShowHide()}>
                            Save Review
                    </Button>
                    </Modal.Footer>
                </Modal>

            </div>
        )
    }

}

export default ViewModal;

但您应该始终创建一个单独的模态组件。

【讨论】:

  • 感谢@shubham-chitransh 我会试一试,让你知道这是怎么回事,非常感谢。
  • 试过了,但得到了这个错误 ``` TypeError: Cannot read property 'showModal' of undefined new ViewModal src/Containers/MainBody/Listing/ViewModal.js:11 ``` 这里是凌晨 3:20 ,睡吧,明天试试这个。
  • 我不得不稍微修改一下你的 ViewModal.js 逻辑,并有 ``` showHide: this.props.showModal 吗? true : false `` 在 componentDidmount() 中。
  • 你说的“但是你应该总是创建一个单独的模态组件”是什么意思。
  • 这意味着您的 Modal 组件应该是独立的,并且不应包含任何其他内容,因此您不需要有条件地显示/隐藏该内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 2014-12-18
  • 2021-05-26
  • 2018-09-10
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
相关资源
最近更新 更多