Skip to content

✅ Error: Too many re-renders. React limits the number of renders to prevent an infinite loop. #19

@jaynamm

Description

@jaynamm

QnA 게시판에서 하나의 게시글의 제목을 클릭해서 들어가면 다음과 같이 에러가 발생한다.

image



동작하는 과정은 아래와 같다.

Qna.js

제목을 클릭하면 onClick() 이벤트 함수가 실행된다.

<table>
    <th>번호</th>
    <th>제목</th>
    <th>작성자</th>
    <th>생성일</th>
    {questions.map((question) => (    
        <tr>
            <td>{question.id}</td>
            <td onClick={() => {onClickQnaViewHandler(question.id)}}>{question.title}</td>
            <td>{question.author}</td>
            <td>{question.createdDate}</td>
        </tr>
    ))}
</table>
  • onClick() 함수
    questionId 값을 가지고 /qna/view 로 이동한다.
const onClickQnaViewHandler = (questionId) => {
    navigate(`/qna/view/${questionId}`, {
        state: questionId
    });
}

QnaView.js

questionId 를 가져와서 API 를 이용해 해당 질문 글을 가져온다.

const location = useLocation();
const getId = location.state;

const [questionId, setQuestionId] = useState("");
const [questionViewData, setQuestionViewData] = useState([]);

setQuestionId(getId);
console.log(questionId);

axios({
    method: "GET",
    url: `/api/qna/question/view/${questionId}`
})
.then((res) => {
    console.log(res.data.question);

    // setQuestionViewData(res.data.question);
})
.catch((err) => {
    console.log(err);
})

일단 동작 과정은 이런데 다시 한번 처음부터 생각해봐야겠다.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions