简体   繁体   English

为什么我的 reactstrap Collapse 不反映我的初始或更改 state

[英]Why does my reactstrap Collapse not reflect my initial or changed state

I am trying to use reactstrap's Collapse to collapse two alternating sections.我正在尝试使用 reactstrap 的Collapse来折叠两个交替的部分。

import React, { Component, Fragment, Suspense } from "react";
import ReactDOM from 'react-dom';
import { PDFViewer } from '@react-pdf/renderer'
import NewWindow from 'react-new-window'
import Config from 'config';
import { Button, Collapse } from 'reactstrap';
import { formatter } from '../common.js'
import { format } from 'date-fns';
import Pagination from '../Pagination';
import "./Order.css";
import Modal from 'react-modal';
import PaymentModal from './paymentModal.js';
import Invoice from './Reports/Invoice';

Modal.setAppElement('#root');

let PageSize = 25;

class Portal extends React.Component {
    constructor() {
        super();
        this.state = {
            name: 'React',
            apiData: [],
            currentPage: 1,
            currentTableData: [],
            orderList: [],
            isOpen: false,
            pdfView: null,
            viewInvoices: true,
            viewOrders: false
        };
    }

    showInvoices() {
        console.log("Show Invoices Clicked")
        this.setState({ viewInvoices: true });
        this.setState({ viewOrders: false });
    }

    showOrders() {
        console.log("Show Orders Clicked")
        this.setState({ viewInvoices: false });
        this.setState({ viewOrders: true });
    }

    async componentDidMount() {        
        console.log('app mounted');

        const tokenString = sessionStorage.getItem("token");
        const token = JSON.parse(tokenString);

        let headers = new Headers({
            "Accept": "application/json",
            "Content-Type": "application/json",
            'Authorization': 'Bearer ' + token.token
        });
        
        const response = await fetch(Config.apiUrl + `/api/Orders/GetAllInvoices`, {
            method: "GET",
            headers: headers
        });
        const json = await response.json();
        console.log(json);
        this.setState({ orderList: json });
    }

    componentDidUpdate(_, prevState) {
        console.log('Component Updated');
        if (prevState.currentPage !== this.state.currentPage || prevState.orderList !== this.state.orderList) {
            const firstPageIndex = (this.state.currentPage - 1) * PageSize;
            const lastPageIndex = firstPageIndex + PageSize;
            this.setState({ currentTableData: this.state.orderList.slice(firstPageIndex, lastPageIndex) });
        }
    }
    
    render() {
        const orders = this.state.orderList;
        const currentTableData = this.state.currentTableData;
        const { isOpen } = this.state;
           

        let onRequestClose = () => {
            this.setState({ isOpen: false });
        }

        let handleClick = () => {
            console.log("Clicked")
            this.setState({ isOpen: true });
        }        

        function handleInvoiceClick(e, invoice) {            
            e.preventDefault();
            console.log(`invoice: ${JSON.stringify(invoice)}`)
            if (this.state.pdfView === null) {
                const headerString = sessionStorage.getItem("header");
                const header = JSON.parse(headerString);
                const buff = new Buffer(header.logoImage, 'base64');                
                let pdf = (       
                    <PDFViewer width="1000" height="600" className="portal">                            
                        <Invoice invoice={invoice} buff={buff} />                            
                    </PDFViewer>
                );
                this.setState({ pdfView: pdf });
            } else {
                this.setState({ pdfView: null });
            }
        }

        handleInvoiceClick = handleInvoiceClick.bind(this);

        return (
            <div id="bootstrap-overrides">
                <h2>Portal</h2>
                <div className="row">
                    <div className="block col-1">
                        <p><button onClick={this.showInvoices.bind(this)}>Invoices</button></p>
                        <p><button onClick={this.showOrders.bind(this)}>Orders</button></p>
                    </div>
                    <div className="block col-2">
                        <br />
                        {this.state.pdfView}
                        <Collapse isOpen={this.state.showInvoices}>
                            <h3>Open Invoices</h3>
                            <h4>A list of completed orders purchased under this account.</h4>
                            <table className="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th className="number">Invoice Number</th>
                                        <th className="date">Invoice Date</th>
                                        <th className="date">Due Date</th>
                                        <th className="amount">Total</th>
                                        <th className="customer">Ship To</th>
                                        <th className="date">Actual Ship Date</th>
                                        <th className="button"></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {currentTableData && currentTableData.map(order =>
                                        <>
                                            <tr key={order.sopnumbe}>
                                                <td><a href="#" onClick={(e) => handleInvoiceClick(e, order)}>{order.sopnumbe}</a></td>
                                                <td>{format(Date.parse(order.invodate), 'MM/dd/yyyy')}</td>
                                                <td>{format(Date.parse(order.duedate), 'MM/dd/yyyy')}</td>
                                                <td>{formatter.format(order.docamnt)}</td>
                                                <td>{order.custname}</td>
                                                <td>{format(Date.parse(order.actlship), 'MM/dd/yyyy')}</td>
                                                <td><Button className="BtnPay" onClick={handleClick}>Pay</Button></td>
                                            </tr>
                                            {isOpen ? <PaymentModal invoice={order} onRequestClose={onRequestClose} /> : null}                                
                                        </>
                                    )}
                                </tbody>
                            </table>                
                            <Pagination
                                className="pagination-bar"
                                currentPage={this.state.currentPage}
                                totalCount={orders.length}
                                pageSize={PageSize}
                                onPageChange={page => this.setState({ currentPage: page })}
                                />
                        </Collapse>
                        <Collapse isOpen={this.state.showOrders}>
                            <h3>Open Orders</h3>
                            <h4>A list of completed orders purchased under this account.</h4>
                        </Collapse>
                    </div>
                </div>
            </div>
        );
    }
}
export default Portal;

The isOpen doesn't seem to be reading my state . isOpen似乎没有阅读我的state The Portal loads with both sections collapsed. Portal加载时两个部分均已折叠。 My two buttons run (I get a console log entry for both) but don't effect the display.我的两个按钮运行(我得到一个控制台日志条目)但不影响显示。 I would think the initial Invoice should be open as well.我认为最初的Invoice也应该是开放的。 Why does my reactstrap Collapse element not reflect my initial or changed state?为什么我的 reactstrap Collapse元素不反映我的初始或更改的 state? Do I need to add anything to componentDidUpdate ?我需要向componentDidUpdate添加任何内容吗? Any tips or advice will be appreciated.任何提示或建议将不胜感激。

reactstrap = reactstrap@9.0.1 reactstrap = reactstrap@9.0.1

I think you are using the wrong variables我认为您使用了错误的变量

<Collapse isOpen={this.state.showInvoices}>
<Collapse isOpen={this.state.showOrders}>

should be应该

<Collapse isOpen={this.state.viewInvoices}>
<Collapse isOpen={this.state.viewOrders}>

The show.. ones are the functions. show..那些是功能。 The isOpen expects a boolean value isOpen需要一个 boolean 值

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

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