React login information cannot be recorded

react writes to the login interface. After entering, it stores user information to form a table form, and then log in again. The form is added. I use localStorage to store information, and express is used as a server, but I can only store values once at a time. I want to be able to record the cumulative information of each login

.

clipboard.png

import React from "react";
import {Segment, Input, Button,} from" semantic-ui-react"
import moment from "moment/moment";

class View extends React.Component {

constructor(props) { //
    super(props);
    this.state = {
        user:"",
        password:"",
        time:moment().format("YYYY-MM-DD HH:mm:ss"),
        if: {user:1234}&&{password:1234}?"":""
    }
    
    this.userChange = this.userChange.bind(this);
    this.passwordChange = this.passwordChange.bind(this);
    this.submit = this.submit.bind(this);
    this.getConnect = this.getConnect.bind(this);
}

userChange(e){

    this.setState({ user : e.target.value })
}

passwordChange(e) {

    this.setState({ password : e.target.value })
}



submit(){
    this.getConnect();
}

getConnect(){ //api
    let text = {user:this.state.user,password:this.state.password,time:this.state.time,if:this.state.if} //
    let send = JSON.stringify(text); //json
    localStorage.setItem("temp2",send);
    fetch(`http://10.10.22.201:8081/password`,{ //Fetch
        method: "POST",
        headers: {"Content-Type": "application/json; charset=utf-8"},
        body: send
    }).then(res => res.json()).then(
        data => {
            if(data.success) {
                window.alert("")
                this.props.history.push("/Page")
            }
            else window.alert("")
        }
    )

}

render(){


    return(
        <div style={{margin:"10px"}}>
            <Segment style={{textAlign:"center"}}>
                <h2></h2>
                <Input
                    id="user"
                    placeholder=""
                    style={{marginBottom:"10px"}}
                    onChange={this.userChange}
                /><br/>
                <Input
                    id="password"
                    type="password"
                    placeholder=""
                    style={{marginBottom:"10px"}}
                    onChange={this.passwordChange}
                /><br/>
                <Button
                    primary
                    content=""
                    style={{marginBottom:"10px"}}
                    onClick={this.submit}
                />
            </Segment>
        </div>
         )
    }

}
export default View;

const http = require ("http");
var express = require (" express");
var app = express ();
var bodyParser = require ("body-parser");

app.use (bodyParser.json ());
app.use (bodyParser.urlencoded ({extended: true}));

)

app.all ("*", function (req, res, next) {

)
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Access-Token");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By"," 3.2.1")
res.header("Content-Type", "application/json;charset=utf-8");
next();

});

app.use (express.static ("public"));

)

app.post ("/ password", function (req, res) {

)
let data = req.body;
console.log(data);
let message1 = {success:true}
let message2 = {success:false}
if(data.user==="1234"&&data.password==="1234"){
    res.send(message1);
}
else res.send(message2);

})

app.get ("/ password", function (req, res) {/ / added code

let myjson = {


}
res.send(myjson)

}
)

var server = app.listen (8081, function () {

)
var host = server.address().address
var port = server.address().port
console.log( " http://%s:%s",host, port )

})

Jul.22,2021

then save the array objects and pay attention to clearing the old localStorage.

let text = {user:this.state.user,password:this.state.password,time:this.state.time,if:this.state.if} //

if(localStorage.getItem("temp2")) {
    let arr = JSON.parse(localStorage.getItem("temp2"));
    arr.push(text);
    localStorage.setItem("temp2", JSON.stringify(arr));
} else {
    localStorage.setItem("temp2", JSON.stringify([text]));
}
Menu