redux 를 사용하지 않은 react.js 의 TO-DO CRUD 구현
C:\SourceSample>npm install -g create-react-app
C:\SourceSample>create-react-app my-app
C:\SourceSample>cd my-app
C:\SourceSample\my-app>yarn start
App.js
import React from "react";
import "./App.css";
import TransactionList from "./components/TransactionList";
function App() {
return <TransactionList />;
}
export default App;
components > TransactionForm.js
import React, { Component } from "react";
class TransactionForm extends Component {
state = {
...this.returnStateObject(),
};
returnStateObject() {
if (this.props.currentIndex === -1)
return {
bAccountNo: "",
iFSC: "",
bName: "",
amount: "",
};
else return this.props.list[this.props.currentIndex];
}
componentDidUpdate(prevProps) {
if (
prevProps.currentIndex !== this.props.currentIndex ||
prevProps.list !== this.props.list
) {
this.setState({ ...this.returnStateObject() });
console.log(prevProps, this.props);
}
}
handleInputChange = (e) => {
this.setState({
[e.target.name]: e.target.value,
});
};
handleSubmit = (e) => {
e.preventDefault();
this.props.onAddOrEdit(this.state);
};
render() {
return (
<form onSubmit={this.handleSubmit} autoComplete="off">
<input
name="bAccountNo"
placeholder="Account Number"
onChange={this.handleInputChange}
value={this.state.bAccountNo}
/>
<br />
<input
name="iFSC"
placeholder="IFSC"
onChange={this.handleInputChange}
value={this.state.iFSC}
/>
<br />
<input
name="bName"
placeholder="A/C Holder Name"
onChange={this.handleInputChange}
value={this.state.bName}
/>
<br />
<input
name="amount"
placeholder="Amount"
onChange={this.handleInputChange}
value={this.state.amount}
/>
<br />
<button type="submit">Submit</button>
</form>
);
}
}
export default TransactionForm;
components > TransactionList.js
import React, { Component } from 'react'
import TransactionForm from './TransactionForm'
class TransactionList extends Component {
state = {
currentIndex: -1,
list: this.returnList()
}
returnList() {
if (localStorage.getItem('transactions') == null)
localStorage.setItem('transactions', JSON.stringify([]))
return JSON.parse(localStorage.getItem('transactions'))
}
handleEdit = (index) => {
this.setState({
currentIndex: index
})
}
handleDelete = (index) => {
var list = this.returnList()
list.splice(index, 1);
localStorage.setItem('transactions', JSON.stringify(list))
this.setState({ list, currentIndex: -1 })
}
onAddOrEdit = (data) => {
var list = this.returnList()
if (this.state.currentIndex === -1)
list.push(data)
else
list[this.state.currentIndex] = data
localStorage.setItem('transactions', JSON.stringify(list))
this.setState({ list, currentIndex: -1 })
}
render() {
return (
<div>
<TransactionForm
currentIndex={this.state.currentIndex}
list={this.state.list}
onAddOrEdit={this.onAddOrEdit}
/>
<hr />
<table>
<tbody>
{this.state.list.map((item, index) => {
return <tr key={index}>
<td>{item.bAccountNo}</td>
<td>{item.bName}</td>
<td>{item.amount}</td>
<td><button onClick={() => this.handleEdit(index)}>Edit</button></td>
<td><button onClick={() => this.handleDelete(index)}>Delete</button></td>
</tr>
})}
</tbody>
</table>
</div>
)
}
}
export default TransactionList
index.css
body {
margin: auto;
width: 50%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-size: 1.5rem;
}
input,
button {
font-size: 1.5rem;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}