Next.js Discord

Discord Forum

react hook not working after submitting

Answered
Chinese Alligator posted this in #help-forum
Open in Discord
Avatar
Chinese AlligatorOP
so as the title say after submitting and adding the comment to the array i can't add a new post , when i click the button submit nothing happens , and it does not call the method

export default function CommentsList({parentId}: {parentId: string}) {
  const [ commentsList, setCommentsList ] = useState<Comments[]>([]);
  const [ imgAvatar, setImgAvatar ] = useState("default.png");

  const {
    register,
    handleSubmit,
    formState: {errors, isSubmitting},
    reset
  } = useForm<TCommentSchema>({
    resolver: zodResolver(commentSchema)
  })

  async function newComment(schema: TCommentSchema){
    console.log("asdwe")
    schema.postId = parentId;
    const res = await addNewComment(schema)
    if(res.errors){
      toast.error(res.errors);
    } else {
      setCommentsList(commentsList.concat(res.post as Comments))
      reset({text: "", postId:""})
    }
  }

  return (
    <div className=' w-full'>
      <div className='flex flex-col'>
        {commentsList.map((c, i) => (
          <Comment key={i} comment={c}/>
        ))} 
      </div>
      <form 
        onSubmit={handleSubmit(newComment)}
        className='flex gap-2 w-full mt-3'>
        <Image 
          className="rounded-full aspect-square"
          src={"/img/avatars/" + imgAvatar} alt={"profile image"} width={45} height={45}/>
        <input 
          {...register("text")}
          className='bg-inherit border-2 border-accent rounded-lg outline-none p-2 w-full focus:border-primary'
          placeholder='Write a comment...'
          type="text" />
        <button type='submit' className='bg-primary rounded-lg px-4 text-black hover:text-white'>
            <BiCommentAdd size="1.2em"/>
        </button>
      </form>
    </div>
  )
}
Answered by Chinese Alligator
solved: stupid error , i wasn't loggin the error of zod, and itseemed that everything was ok but it wasn't
View full answer

1 Reply

Avatar
Chinese AlligatorOP
solved: stupid error , i wasn't loggin the error of zod, and itseemed that everything was ok but it wasn't
Answer