<부모 컴포넌트>
function App(a,i) {
let [title, settitle] = useState(['BLOG에 오신걸 환영합니다.'])
<자식 컴포넌트1>
function Domestic(props) {
let copy = [...props.title];
useEffect(()=>{
copy.unshift('국내여행 페이지 입니다.');
props.settitle(copy);
},[])
return
{
copy.map(function(a,i){
return(
<div className='Pdomestic'>
<h5>{copy[i]}</h5>
<p>10월 1일 발행</p>
}
<자식 컴포넌트2>
function Modal(props)
return
<div className='modal'>
<p>제목 : <input name="topic" onChange={(e)=>{setTopic(e.target.value)}}></input></p>
<p>내용 : </p><textarea name="context" onChange={(e)=>{setInput(e.target.value)}}></textarea><hr/>
<button onClick={()=>{
axios.post('http://221.xxx.xxx.xx:5500/add', {
topic : Topic,
context : Input
});
axios.get('http://221. xx.xxx.xx:5500/add',
}).then((post)=>{
let copy = [...props.title];
copy.unshift(post.posts[2].제목);
props.settitle(copy);
})
props.navigate(-1)
}}>글 올리기</button>
<nodejs>
app.get('/add', function(req,res){
db.collection('POST').find().toArray(function(에러, 결과){
if(에러) return console.log(에러);
console.log(결과);
res.send({posts : 결과});
})
})
시스템구조 설명
1. 자식 컴포넌트 2에서 제목과 내용을 입력하여 글 올리기를 누르면
서버로 post요청 후 다시 get 요청하여 id가 2인 데이터의 제목을 부모 컴포넌트에 있는 변수 title에
추가시켜줌
2. 자식 컴포넌트 1에서 map 함수에 있는 copy[i]에서 추가된 데이터의 제목을 보여주게 함
문제
1. id가 2인 데이터의 제목이 출력이 안됨. 10월 1일은 잘 출력됨.( 다른 건 출력 잘됨. post는 문제없음) -> 파라미터 형태의 문제??
2. 서버로 post get요청을 하였는데도 새로고침 후에는 다시 리셋됨. -> html파일을 다시 읽어서 그러는 건데 그럼 컴포넌트 순서 문제??
도무지 해결이 힘듭니다..... 코딩마스터님 부디 도와주십시오ㅠ