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

home2 게시판 Next.js 게시판 클라이언트 컴포넌트에서 session 사용하기

클라이언트 컴포넌트에서 session 사용하기

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

    송지오
    참가자
    'use client'
    사용하면 await , async 사용 못 한다는데 let session = await getServerSession(authOptions); 이 문장 기능이 필요한데 어떻게 대체하나요??
    아래 코드에서 axios로 로그인중인 사용자 이름까지 서버로 보내고싶은데 let session = await getServerSession(authOptions)
    정의 하고 session.user.username 보내려고하면
    Error: async/await is not yet supported in Client Components, only Server Components. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.
    라고 뜨네요
    
     
    'use client'
    import axios from "axios";
    import { useState } from "react";
    export default function ListClient() {
        const [todo, setTodo] = useState("");
        const [deadline, setDeadline] = useState("");
        const todoSubmitHandler = () => {
            axios.post("http://localhost:8080/api/post-data", { todo, deadline})
                .then(res => {
                    console.log(res);
                })
                .catch(err => {
                    console.error(err);
                });
        };
    
        return (
            <div className="list-container">
                <div className="input-container">
                    <label>할일 </label>
                    <input
                        type="text"
                        onChange={(e) => setTodo(e.target.value)}
                        autoComplete="off"
                    />
                    <p>&nbsp;&nbsp;&nbsp;</p>
                    <label>기한 </label>
                    <input
                        type="text"
                        placeholder="월/일"
                        onChange={(e) => setDeadline(e.target.value)}
                        maxLength={5}
                        autoComplete="off"
                    />
                    <p>&nbsp;&nbsp;&nbsp;</p>
                    <button onClick={todoSubmitHandler}>추가</button>
                </div>
            </div>
        );
    }
    자세한 코드 확인 필요하시면 https://github.com/KingZio/codingapple-next 이거 봐주세여 위에 코드는 app/list/page.js입니다
    • 이 게시글은 송지오에 의해 1 년, 1 월 전에 수정됐습니다.
    • 이 게시글은 송지오에 의해 1 년, 1 월 전에 수정됐습니다.
    #124968

    codingapple
    키 마스터
    client component에선 useSession 사용해서 출력합시다 아니면 서버컴포넌트에서 props로 보내도 됩니다
    #124979

    송지오
    참가자
    await, async는 그대로 쓰나요??
    #124992

    송지오
    참가자
    props 사용하여 해결 했습니당!! 감사합니다
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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