• 로그인
  • 장바구니에 상품이 없습니다.

home2 게시판 Next.js 게시판 댓글기능 구현 중에 input 초기화 질문...

댓글기능 구현 중에 input 초기화 질문...

3 글 보임 - 1 에서 3 까지 (총 3 중에서)
  • 글쓴이
  • #122822

    김동아
    참가자
    input에 값을 입력하고 '댓글 전송'을 누르면 input 값이 초기화되게 하려고
    setComment('');라고 작성했는데 왜 안될까요 ㅠㅠ?
    
    
    'use client'
    import { useState,useEffect } from "react"
    export default function Comment(props){
    let [comment,setComment] = useState('');
    let [data,setData]=useState([])
    useEffect(()=>{
    fetch(`/api/comment/list?id=${props._id}`)
     .then(r=>r.json())
     .then((result)=>{
    console.log(result)
    setData(result)
     })
     },[])
    const handleCommentSubmit=()=>{
     
    fetch('/api/comment/new',{
    method:'POST',
    body:JSON.stringify({comment: comment, _id:props._id})
     }).then(()=>{
    fetch(`/api/comment/list?id=${props._id}`).then(r=>r.json())
     .then(result=>{
    setData(result);
    setComment(''); // 입력값을 초기화
     })
     })
     .catch((error)=>console.log(error))
     
     }
    return(
    <div style={{padding:"15px"}}>
    <div>댓글</div>
    <div>
    <div style={{padding:"5px"}}>
    <input onChange={(e)=>{ setComment(e.target.value) }} style={{width:"500px",
    fontSize:"20px"}}/>
    <button onClick={handleCommentSubmit} style={{width:"100px",fontSize:"20px",marginLeft:"10px"}}>댓글전송</button>
    </div>
     
    <ul style={{ listStyleType: "none", padding: "0" }}>
    
  • {data.length > 0 ?
    data.map((a,i)=>
     
    <profile style={{height: "auto",
    display: "flex",
    border:"0.5px solid gray",
    padding:"5px"}}>
    <div>
    < img src='/user.png' style={{ width: "45px", paddingTop: "15px" }} alt="user" />
    </div>
    <info style={{ padding:"0 0 0 10px",
    display: "flex",flexDirection:"column"}}>
    <p style={{ borderBottom: "1px solid lightgray"}}></p>
    <div style={{ display: "flex" }}>
    <p key={i}>{a.content}</p>
    <span>{a.author_name}</span>
     
    </div>
    </info>
    </profile>
     ): '댓글 없음'} 
    
    
    </div>
    </div>
     )
    }
    • 이 게시글은 김동아에 의해 1 년, 1 월 전에 수정됐습니다.
    • 이 게시글은 김동아에 의해 1 년, 1 월 전에 수정됐습니다.
    #122854

    codingapple
    키 마스터
    input에 value={comment} 넣어봅시다
    #123279

    김동아
    참가자
    오오 해결했습니다!감사합니다
3 글 보임 - 1 에서 3 까지 (총 3 중에서)
  • 답변은 로그인 후 가능합니다.

About

현재 월 700명 신규수강중입니다.

  (09:00~20:00) 빠른 상담은 카톡 플러스친구 코딩애플 (링크)
  admin@codingapple.com
  이용약관
ⓒ Codingapple, 강의 예제, 영상 복제 금지
top

© Codingapple, All rights reserved. 슈퍼로켓 에듀케이션 / 서울특별시 강동구 고덕로 19길 30 / 사업자등록번호 : 212-26-14752 온라인 교육학원업 / 통신판매업신고번호 : 제 2017-서울강동-0002 호 / 개인정보관리자 : 박종흠