javascript - React Js 单击时反向反转数组

标签 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

为什么会这样做以及如何解决这个问题。

最佳答案

不要在 render 方法中反转它。每次重新渲染时,您都会反转它。因此,当您打开模式窗口时,可能会发生第二次反转。

在这里做一次

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

关于javascript - React Js 单击时反向反转数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591212/

相关文章:

html - 所有浏览器的css线性渐变效果

c++ - 有限制的最大数组和

c++ - 是否可以在 C++ 的成员初始化列表中初始化数组?

arrays - 如何分配 channel 数组

javascript - "THREE.NearestFilter"或"THREE.LinearFilter"导致黑色平面

javascript - 更改所需 React 组件的原型(prototype)

javascript - Tinymce编辑器图片上传插件添加完整图片URL

php - 使用 javascript 和 ORM 仅使用 json 数据构建 Web 应用程序

html - text-center 和 text-right 在 bootstrap 中不起作用

java - 有没有办法从java中读取.fdt/.fdx/.fdxt ftile中的文本?