Last active
November 19, 2020 19:02
-
-
Save TheArhaam/b35f4d147afb4767fdc8cb493afd5e1b to your computer and use it in GitHub Desktop.
randomlist App.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEffect } from 'react' | |
import './App.css'; | |
import axios from 'axios'; | |
axios.defaults.baseURL = "http://localhost:5000/" | |
const App = () => { | |
const [list, setList] = useState([]); | |
useEffect(() => { | |
axios.get('/list') | |
.then((res) => { setList(res.data) }) | |
.catch((err) => { alert(err) }) | |
}, []) | |
const onSubmit = (e) => { | |
e.preventDefault(); | |
axios.post("/list/item", { item: e.target.itemName.value }) | |
.then((res) => { | |
var temp = [...list]; | |
temp.push({ name: e.target.itemName.value }); | |
setList(temp); | |
}) | |
.catch((err) => { | |
alert(err); | |
}) | |
} | |
return ( | |
<div className="App"> | |
<form onSubmit={onSubmit}> | |
<input type="text" name="itemName" /> | |
<input type="submit" /> | |
</form> | |
<h3>LIST: </h3> | |
{list.map((item) => { | |
return ( | |
<div>{item.name}</div> | |
); | |
})} | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment