Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 서버설정
- GIT
- 데비안
- d
- 자료구조와함께배우는알고리즘입문
- resttemplate
- 자료구조와 함께 배우는 알고리즘 입문
- 구멍가게코딩단
- 코드로배우는스프링부트웹프로젝트
- 처음 만나는 AI수학 with Python
- 목록처리
- 이터레이터
- iterator
- 알파회계
- 코드로배우는스프링웹프로젝트
- /etc/network/interfaces
- 스프링 시큐리티
- 친절한SQL튜닝
- 티스토리 쿠키 삭제
- 처음 만나는 AI 수학 with Python
- 선형대수
- ㅒ
- network configuration
- 네트워크 설정
- baeldung
- Kernighan의 C언어 프로그래밍
- 자바편
- 스프링부트핵심가이드
- 페이징
- 리눅스
Archives
- Today
- Total
bright jazz music
생활코딩: 리액트 프로그래밍 CRUD 기본 본문
import logo from './logo.svg';
import './App.css';
import { useState } from 'react';
function Header(props){
return(
<header>
<h1><a href="/" onClick={(event)=>{
event.preventDefault()
props.onChangeMode()
}}>{props.title}</a></h1>
</header>
)
}
function Nav(props){
const lis = [];
for(let i=0; i<props.topics.length; i++){
let t = props.topics[i];
lis.push(
<li key={t.id} >
<a id={t.id} href={'/read/'+t.id} onClick={(event)=>{
event.preventDefault();
props.onChangeMode(Number(event.target.id));
/**
mode==='READ'인 경우 타입이 같아야 하기 때문에 Number를 사용하여 숫자로 바꿔줌.
for(let i=0; i<topics.length;i++){
if(topics[i].id === id) {
*/
}}>{t.title}</a>
</li>
)
}
return (
<nav>
<ol>
{lis}
</ol>
</nav>
)
}
function Article(props){
return (
<article >
<h2>{props.title}</h2>
<p>{props.body}</p>
</article>
)
}
function Create(props) {
return(
<article>
<h2>Create</h2>
<form onSubmit={(event)=>{
event.preventDefault();
const title = event.target.title.value;
const body = event.target.body.value;
props.onCreate(title, body)
}}>
<p><input type="text" name ='title' placeholder='title'></input></p>
<p><textarea name ='body' placeholder='body'></textarea></p>
<p><input type='submit' value='Create'></input></p>
</form>
</article>
)
}
function Update(props){
const [title, setTitle] = useState(props.title)
const [body, setBody] = useState(props.body)
return(
<article>
<h2>Update</h2>
<form onSubmit={(event)=>{
event.preventDefault();
const title = event.target.title.value
const body = event.target.body.value
props.onUpdate(title, body);
}}>
<p><input type='text' name='title' value={title} placeholder='title' onChange={(event)=>{
setTitle(event.target.value)
}}></input></p>
<p><textarea name='body' value={body} placeholder='body' onChange={(event)=>{
setBody(event.target.value)
}}></textarea></p>
<p><input type='submit' value='Update'></input></p>
</form>
</article>
)
}
function App() {
const basicList=[
{id:1, title:'html', body:'html is....'},
{id:2, title:'css', body:'css is....'},
{id:3, title:'js', body:'js is....'}
]
const [mode, setMode] = useState('WELCOME');
const [id, setId] = useState(null);
const [nextId, setNextId] = useState(4);
const [topics, setTopics] = useState(basicList)
let content = null;
let contextControl = null;
if(mode === 'WELCOME') {
content = <Article title="Welcome" body="Hello, Web"></Article>
}else if(mode === "READ"){
let title, body = null;
for(let i=0; i<topics.length;i++){
if(topics[i].id === id) {
title = topics[i].title
body = topics[i].body
}
}
content = <Article title={title} body={body}></Article>
contextControl = <>
<li>
<a href={'/update/' + id} onClick={(event) => {
event.preventDefault();
setMode('UPDATE')
}}>
Update
</a>
</li>
<li>
<input type='button' value='Delete' onClick={(event)=>{
const newTopics = []
for(let i=0; i< topics.length; i++){
if(topics[i].id != id){
newTopics.push(topics[i])
}
// 위 코드가 반복되면 아이디가 같은 요소(현재 글)만 배열에 추가되지 않게 된다.
}
setTopics(newTopics) //현재 글만 제외된 배열을 setTopic에 넣는다.
setMode('WELCOME')
}}></input>
</li>
</>
}else if(mode === 'CREATE'){
content = <Create onCreate={(_title, _body)=>{
const newTopic = {id:nextId, title:_title, body:_body};
const newTopics = [...topics]
newTopics.push(newTopic)
setTopics(newTopics)
setMode('READ')
setId(nextId);
setNextId(nextId+1)
}}></Create>
}else if(mode === 'UPDATE') {
let title, body = null;
for(let i=0;i<topics.length;i++){
if(topics[i].id === id) {
title = topics[i].title
body = topics[i].body
}
content = <Update title={title} body={body} onUpdate={(_title, _body)=>{
console.log(_title, _body)
const updatedTopic = {id:id, title:_title, body:_body}
const newTopics = [...topics]
for(let i=0;i<newTopics.length;i++) {
if(newTopics[i].id === id) {
newTopics[i] = updatedTopic
break;
}
}
setTopics(newTopics);
setMode('READ')
}}>
</Update>
}
}
return (
<div>
<Header title='WEB' onChangeMode={()=>{
setMode('WELCOME')
}}></Header>
<Nav topics={topics} onChangeMode={(_id)=>{
setMode("READ");
setId(_id)
}}></Nav>
{content}
<ul>
<li>
<a href='/create' onClick={(event)=>{
event.preventDefault();
setMode('CREATE');
}}>Create</a>
</li>
{contextControl}
</ul>
</div>
);
}
export default App;
'Framework > ReactJs' 카테고리의 다른 글
생활코딩: styled-components (0) | 2023.05.16 |
---|---|
생활코딩: React-Router-DOM 실습 (0) | 2023.05.15 |
리액트 앱과 백엔드 서버 연동 테스트4 (+ docker) (0) | 2023.04.09 |
리액트 앱과 백엔드 서버 연동 테스트3 (+ Nginx) (0) | 2023.04.09 |
리액트 앱과 백엔드 서버 연동 테스트 2 (0) | 2023.04.08 |
Comments