React Js Reversed数组在点击时反转

时间:2017-08-09 12:51:20

标签: javascript html arrays reactjs jsx

所以我有一个从数据库中取出的数组,我已将其反转,以便当您单击页面上的一个按钮时,最新的数据库条目将显示在顶部,该按钮打开一个小模式样式窗口,显示的数组翻转并转到由于某种原因从最旧到最新,我不知道为什么。

这是代码:

import React, { Component } from 'react';
import axios from 'axios';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import RaisedButton from 'material-ui/RaisedButton';
import { Modal, ModalHeader, ModalTitle, ModalClose, ModalFooter, ModalBody } from 'react-modal-bootstrap';
import TextField from 'material-ui/TextField';

class ViewNoteDetails extends Component {
    constructor(props) {
        super(props);

        this.NewNote = this.NewNote.bind(this);
        this.handleChange = this.handleChange.bind(this);
    }

    state = {
        Case: this.props.Case,
        notesToShow: this.props.Case.slice(0, parseInt(this.props.AmountToShow, 10)),
        isOpen: false,
        note: '',
    }

    NewNote() {
        const self = this;
        if (this.state.note !== '') {
            axios.post('http://******'
                + window.location.href.split('view/')[1]
                + '/' + self.state.note
            ).then(function (res) {
                if (res.data === "Updated") {
                    self.hideModal();
                    window.location.reload();
                }
            })
            .catch(function (error) {
                console.log(error);
            });
        } else {
            window.alert("Cannot Enter A Blank Note")
        }
    }

    handleChange(e) {
        this.setState({
            [e.target.name]: e.target.value
        });
    }

    openModal = () => {
        this.setState({
            isOpen: true
        });
    };
    hideModal = () => {
        this.setState({
            isOpen: false
        });
    };

    render() {

        const pullRight = {
            float: "right",
            display: "inline",
        }
        const inline = {
            display: "inline",
        }
        const Overflow = {
            overflow: "auto",
            width: "100%",
            height: "50vw",
        }

        return (
            <MuiThemeProvider>
                <div>
                    <Modal isOpen={this.state.isOpen} onRequestHide={this.hideModal}>
                        <ModalHeader>
                            <ModalClose onClick={this.hideModal} />
                            <ModalTitle>Enter Your Note</ModalTitle>
                        </ModalHeader>
                        <ModalBody>
                            <TextField hintText="Enter Note" name="note" fullWidth={true} onChange={this.handleChange} />
                        </ModalBody>
                        <ModalFooter>
                            <RaisedButton label="Save Note" secondary={true} onClick={() => this.NewNote()} />&nbsp;
                            <RaisedButton label="Cancel Note" secondary={true} onClick={() => this.hideModal()} />
                        </ModalFooter>
                    </Modal>
                    <br />
                    <h1 id="TOP" style={inline}>Note Details</h1>
                    <RaisedButton label="New Note" style={pullRight} secondary={true} onClick={() => this.openModal()} />
                    <br />
                    <br />
                    <div style={Overflow}>
                        {this.state.notesToShow.reverse().map(function (note, index) {
                            return (
                                <div key={note.NotePK}>
                                    <p className="well well-lg">
                                        {note.CreationDate.split('T')[0]} @ {note.CreationDate.split('T')[1].split('.')[0]} : {note.Note}
                                    </p>
                                </div>
                            );
                        })}
                    </div>
                    <br />
                </div>
            </MuiThemeProvider>
        );
    }
}

*****正在取代数据库位置

单击新笔记按钮时,订单会在呈现的笔记上反转

所以如果是:

注意1 笔记2 注3

点击新笔记后会改为

注意3 笔记2 注1

为什么会这样做,我该如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}方法中将其撤消。每次重新渲染时,您都会render它。所以发生的事情可能是当你打开模态窗口时第二次反转。

在这里做一次

reverse