简体   繁体   English

React - 如何将数据传递到 Modal(引导程序)

[英]React - how to pass data into Modal (bootstrap)

I'm trying to pass data into Modal (bootstrap) popup and display some data.我正在尝试将数据传递到模态(引导程序)弹出窗口并显示一些数据。 I have a list of orders with a button 'display info', and every button that i press should display on the popup (Modal) diffrent data.我有一个带有“显示信息”按钮的订单列表,我按下的每个按钮都应该显示在弹出窗口(模式)不同的数据上。

My question is how should i pass the data to the Modal?我的问题是我应该如何将数据传递给模态?

this line <Button variant="primary" onClick={() => {this.handleModal(index)}}> Items info</Button> should trigger the Modal.这行<Button variant="primary" onClick={() => {this.handleModal(index)}}> Items info</Button>应该触发模式。 In the handleModal function it passes the order index.在 handleModal function 它传递了订单索引。 And then i update the index on the setState of the handleModal function. The Modal open but nothing passes to it.然后我更新handleModal function 的setState上的索引。模态打开但没有传递给它。

I'm not sure that this is the correct way of doing it.我不确定这是正确的做法。 Also the Modal is inside the loop of the filteredOrders, should i move the Modal outside the loop? Modal 也在 filteredOrders 的循环内,我应该将 Modal 移到循环外吗? And if yes, how should i do that and where?如果是,我应该怎么做以及在哪里?

import React, {useState} from 'react';
import './App.scss';
import {createApiClient, Item, Order} from './api';
import Modal from 'react-bootstrap/Modal';
import Button from 'react-bootstrap/Button';
import 'bootstrap/dist/css/bootstrap.min.css'
export type AppState = {
  orders?: Order[],
search: string;
show:boolean;
item?: Item,
order_id: number,
}


const api = createApiClient();

export class App extends React.PureComponent<{}, AppState> {


state: AppState = {
    search: '',
    show:false,
    order_id: 0,
};

searchDebounce: any = null;


async componentDidMount() {
    this.setState({
        orders: await api.getOrders()
    });
}
async getItem(itemID: string){
    this.setState({
        item: await api.getItem(itemID)
    });
}

render() {
    const {orders} = this.state;
    return (
        <main>
            <h1>Orders</h1>
            <header>
                <input type="search" placeholder="Search" onChange={(e) => this.onSearch(e.target.value)}/>
            </header>
            {orders ? <div className='results'>Showing {orders.length} results</div> : null}
            {orders ? this.renderOrders(orders) : <h2>Loading...</h2>}

        </main>
    )
}
handleModal(index: number)
{
    this.setState({
        show:true,
        order_id: index,
    })
}
handleClose () {
    this.setState({show: false})
}
renderOrders = (orders: Order[]) => {

    const filteredOrders = orders
        .filter((order) => (order.customer.name.toLowerCase() + order.id).includes(this.state.search.toLowerCase()));
    const requiredItem = this.state.order_id;
    const modelData = filteredOrders[requiredItem];
    return (
        <div className='orders'>
            {filteredOrders.map((order,index) => (
                <div className={'orderCard'}>
                    <div className={'generalData'}>
                        <h6>{order.id}</h6>
                        <h4>{order.customer.name}</h4>
                        <h5>Order Placed: {new Date(order.createdDate).toLocaleDateString()}</h5>
                    </div>
                    <div className={'fulfillmentData'}>
                        <h4>{order.itemQuantity} Items</h4>
                        <img src={App.getAssetByStatus(order.fulfillmentStatus)}/>
                        {order.fulfillmentStatus !== 'canceled' &&
                        <a href="#" onClick={() => this.ChangeStatus(order)}>Mark
                            as {order.fulfillmentStatus === 'fulfilled' ? 'Not Delivered' : 'Delivered'}</a>

                        }


                    </div>
                    <div className={'extraData'}>

                        <Button variant="primary" onClick={() => {this.handleModal(index)}}> Items info</Button>

                        <Modal show={this.state.show} >

                            {/*{console.log(modelData)}*/}
                            {/*<Modal.Header closeButton>*/}
                            {/* <Modal.Title>Item Info</Modal.Title>*/}
                            {/*</Modal.Header>*/}
                            <Modal.Body>
                              { console.log(modaelData) }
                            </Modal.Body>
                            <Modal.Footer>

                                <Button onClick={() =>{ this.handleClose()}}>
                                    Close
                                </Button>

                            </Modal.Footer>
                        </Modal>
                    </div>
                    <div className={'paymentData'}>
                        <h4>{order.price.formattedTotalPrice}</h4>
                        <img src={App.getAssetByStatus(order.billingInfo.status)}/>
                    </div>
                </div>
            ))}
        </div>
    )
};

} }

export default App;导出默认应用程序;

I don't think you need to pass data to the Modal, but rather compose the Modal with the data in the first place.我认为您不需要将数据传递给 Modal,而是首先将 Modal 与数据组合在一起。 It is currently empty.目前是空的。 Then you can continue to hide/show the complete Modal with handleModal .然后您可以继续使用handleModal隐藏/显示完整的模态框。

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

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