강의

멘토링

커뮤니티

Cộng đồng Hỏi & Đáp của Inflearn

Hình ảnh hồ sơ của suyeondv1340
suyeondv1340

câu hỏi đã được viết

Lập trình React thực tế

Tạo chức năng chọn với chọn lại

저도 ReferenceError가 나는데 원인을 못 찾았습니다...

Viết

·

432

0

ReferenceError: Cannot access 'friendsWithAgeShowLimit' before initialization

(anonymous function)
C:/Users/la21/OneDrive/바탕 화면/chapter6_using_react-redux/src/friend/container/FriendMain.js:17
  14 | ] = useSelector((state) => {
15 | const { ageLimit, showLimit, friends } = state.friend;
16 | const friendsWithAgeLimit = friends.filter((item) => item.age <= ageLimit);
> 17 | return [
| ^ 18 | ageLimit,
19 | showLimit,
20 | friendsWithAgeLimit,

이렇게 에러가 났는데요. 

혼자서 찾아봤는데도 원인을 못 찾았습니다.

import 할 때 js 확장자 다 없이 했고

밑에 분처럼 node_modules와 package_lock.json 삭제 후 다시 install 했는데도 계속 같은 에러가 나고 있습니다. 

짐작가시는 원인이 있으실까요? 

혹시 몰라서 FriendMain.js  코드 첨부합니다.

import { getNextFriend } from "../../common/mockData";
import { addFriend, setAgeLimit, setShowLimit } from "../state";
import FriendList from "../component/FriendList";
import NumberSelect from "../component/NumberSelect";
import { shallowEqual, useSelector, useDispatch } from "react-redux";
import { MAX_AGE_LIMIT, MAX_SHOW_LIMIT } from "../common";

export default function FriendMain() {
  const [
    ageLimit,
    showLimit,
    friendsWithAgeLimit,
    friendsWithAgeShowLimit,
  ] = useSelector((state) => {
    const { ageLimit, showLimit, friends } = state.friend;
    const friendsWithAgeLimit = friends.filter((item) => item.age <= ageLimit);
    return [
      ageLimit,
      showLimit,
      friendsWithAgeLimit,
      friendsWithAgeShowLimit.slice(0, showLimit),
    ];
  }, shallowEqual);
  const dispatch = useDispatch();

  function onAdd() {
    const friend = getNextFriend();
    dispatch(addFriend(friend));
  }

  return (
    <div>
      <button onClick={onAdd}>친구 추가</button>
      <NumberSelect
        onChange={(v) => dispatch(setAgeLimit(v))}
        value={ageLimit}
        options={AGE_LIMIT_OPTIONS}
        postfix="세 이하만 보기"
      />
      <FriendList friends={friendsWithAgeLimit} />
      <NumberSelect
        onChange={(v) => dispatch(setShowLimit(v))}
        value={showLimit}
        options={SHOW_LIMIT_OPTIONS}
        postfix="명 이하만 보기(연령 제한 적용)"
      />
      <FriendList friends={friendsWithAgeShowLimit} />
    </div>
  );
}

const AGE_LIMIT_OPTIONS = [15, 20, 25, MAX_AGE_LIMIT];
const SHOW_LIMIT_OPTIONS = [2, 4, 6, MAX_SHOW_LIMIT];
reactredux

Câu trả lời 2

0

suyeondv1340님의 프로필 이미지
suyeondv1340
Người đặt câu hỏi

답변이 너무 늦어 죄송합니다. 

저의 오타였다니 너무 부끄럽네요. 

봐주셔서 감사합니다ㅜㅜ

0

landvibe님의 프로필 이미지
landvibe
Người chia sẻ kiến thức

안녕하세요
아래처럼 수정하시면 될 것 같네요

friendsWithAgeShowLimit.slice(0, showLimit)
==>
friendsWithAgeLimit.slice(0, showLimit)

Hình ảnh hồ sơ của suyeondv1340
suyeondv1340

câu hỏi đã được viết

Đặt câu hỏi