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

home2 게시판 React 게시판 상품목록을 store로 빼다가 생긴 오류

상품목록을 store로 빼다가 생긴 오류

  • 이 주제에는 1개 답변, 2명 참여가 있으며 codingapple2 년 전에 전에 마지막으로 업데이트했습니다.
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 글쓴이
  • #93063

    오선화
    참가자
    안녕하세요 선생님
    처음에 data.js파일로 넣은 상품목록과 axios로 불러온 목록을 store.js로 따로 빼다가
    오류가 생겨서 질문 드립니다.
    
    Main.js
    
    import { Container, Row, Button } from "react-bootstrap";
    import Shoes from "../components/Shoes";
    import { useState } from "react";
    import { useDispatch } from "react-redux";
    import { addMoreItem } from "../store/productList";
    const Main = ({ shoes, loading }) => { // App.js에서 useSelector로 store.js의 데이터를 가져온 후 props로 전달
        const [more, setMore] = useState(0);
        const dispatch = useDispatch();
        return (
            <>
                <div
                    className="main-bg"
                    style={{
                        background: `url(${process.env.PUBLIC_URL}'/images/bg.png') center center no-repeat`,
                        backgroundSize: "cover",
                    }}
                ></div>
                <Container>
                    <Row md={3}>
                        {shoes.map((item, idx) => (
                            <Shoes key={item.id} id={item.id} title={item.title} price={item.price} content={item.content} idx={idx} />
                        ))}
                    </Row>
                    <p
                        style={{
                            textAlign: "center",
                            marginTop: "30px",
                            marginBottom: "50px",
                        }}
                    >
                        {more == 2 ? (
                            <span>No more products</span>
                        ) : loading ? (
                            <span>loading...</span>
                        ) : (
                            <Button
                                variant="light"
                                onClick={() => {
                                    dispatch(addMoreItem(more));
                                    setMore(more + 1);
                                }}
                            >
                                More
                            </Button>
                        )}
                    </p>
                </Container>
            </>
        );
    };
    export default Main;
     
    
    /store/productList.js
    
    import { createSlice } from "@reduxjs/toolkit";
    import data from "../data";
    import axios from "axios";
    const productList = createSlice({
        name: "list",
        initialState: { data, isLoading: false }, // isLoading: Main.js의 로딩 useState 대신 삽입
        reducers: {
            addMoreItem(state, action) { // 더보기 눌렀을 때 실행
                state.isLoading = true;
                setTimeout(() => {
                    axios
                        .get(`https://codingapple1.github.io/shop/data${action.payload + 2}.json`)
                        .then((result) => {
                            state.data = [...state.data, ...result.data];
                            state.isLoading = false;
                        })
                        .catch((error) => {
                            console.log(error);
                        });
                }, 1000);
            },
        },
    });
    export const { addMoreItem } = productList.actions;
    export default productList;
    --------------------------------------
    더보기 버튼 눌렀을 때 에러
    TypeError: Cannot perform 'get' on a proxy that has been revoked
    
    
    확인부탁드립니다ㅠㅠ
    감사합니다
    
    
    
    
    #93083

    codingapple
    키 마스터
    리듀서안에선 ajax요청못해서 useEffect안에서 ajax요청하고 그걸 redux store에 저장하라고 코드짭시다
2 글 보임 - 1 에서 2 까지 (총 2 중에서)
  • 답변은 로그인 후 가능합니다.

About

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

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

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