Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 警告:Can';t在未安装的组件上执行React状态更新。useEffect清理功能_Javascript_Reactjs_Promise - Fatal编程技术网

Javascript 警告:Can';t在未安装的组件上执行React状态更新。useEffect清理功能

Javascript 警告:Can';t在未安装的组件上执行React状态更新。useEffect清理功能,javascript,reactjs,promise,Javascript,Reactjs,Promise,如何解决这个问题 //ReviewProductDetail.jsx: 第一次从此组件获取页面时 import React, { useEffect, useState } from 'react'; import RatingCardProductDetail from '../../components/RatingCardProductDetail'; import "./style.sass"; import FilterReviewProductDetail from '../../

如何解决这个问题

//ReviewProductDetail.jsx:

第一次从此组件获取页面时

import React, { useEffect, useState } from 'react';
import RatingCardProductDetail from '../../components/RatingCardProductDetail';
import "./style.sass";
import FilterReviewProductDetail from '../../components/FilterReviewProductDetail';
import { Row, Col, Pagination, Spin } from 'antd';
import LatestReview from '../../components/LatestReview';
import strings from '../../localization/localization';
import Product from '../../repository/Product';

function ReviewProductDetail({ productId }) {
    const [reviewRatingDetail, setReviewRatingDetail] = useState({})
    const [reviewRating, setReviewRating] = useState([])
    const [notFound, setNotFound] = useState(false)
    const [loading,setLoading] = useState(false)

    useEffect(() => {
        getReviewRatingDetail();
        getReviewRating();
    }, [])


    async function getReviewRatingDetail() {
        let reviewRatingDetail = await Product.reviewRatingDetail({
            productId: productId
        })
        if (reviewRatingDetail.status === 200) {
            setReviewRatingDetail(reviewRatingDetail)
        } else {
            setReviewRatingDetail({})
        }
    }

    async function getReviewRating() {
        let reviewRating = await Product.reviewRating({
            productId: productId,
            loading: setLoading
        })
        if (reviewRating.status === 200) {
            setReviewRating(reviewRating)
            setNotFound(false)
        } else {
            setReviewRating([])
            setNotFound(true)
        }
    }

    return (
        <Spin spinning={loading}>
            <div className="mp-review-product-detail">
                {notFound ?
                    <div className="mp-product-detail__not-found">
                        <span>
                            Belum ada ulasan untuk produk ini
                        </span>
                    </div> :
                    <React.Fragment>
                        <RatingCardProductDetail reviewRatingDetail={reviewRatingDetail} />
                        <Row>
                            <Col md={17} offset={3}>
                                <div className="mp-review-product-detail__filter">
                                    <FilterReviewProductDetail />
                                </div>
                            </Col>
                        </Row>
                        <h3>{strings.latest_review}</h3>
                        {reviewRating.review &&
                            reviewRating.review.map((review, i) => {
                                return <LatestReview key={i} review={review} />
                            })}
                        <Pagination
                            className="mp-pagination-review-product-detail"
                            defaultCurrent={1}
                            total={5} />
                    </React.Fragment>}
            </div>
        </Spin>
    );
}; 

export default ReviewProductDetail;
/**不带令牌的服务api getWithoutToken*/


export const apiGetWithoutToken = (url, params = null) => {
  return serviceWithoutToken().get(url, {
    params: params
  });
};

export const serviceWithoutToken = () => axios.create({
  baseURL: REACT_APP_API_SERVICE,
  timeout: 60 * 4 * 1000,
  headers: {
    "Content-Type": `application/json`,
  }
});

我想向你寻求帮助,如何消除这些错误标记。请直接给出一个示例,谢谢

尝试使用效果添加以下内容:

useEffect(() => {
    getReviewRatingDetail();
    getReviewRating();
}, [productId])

或者整体函数ReviewProductDetail use use Callback()

请详细解释您尝试了什么以及问题的具体原因。我想消除上图中的错误通知,我该怎么做?@PutraIrawan但您尝试了什么?这是因为您的代码在卸载状态变量后正在调用状态变量。您是否调查了发生这种情况的原因/方式?您的
useffect
hook是否返回清理函数?
useEffect(() => {
    getReviewRatingDetail();
    getReviewRating();
}, [productId])