简体   繁体   English

组件重新渲染时数组为空

[英]array empty when component rerenders

I've tried searching, tried different approaches in the code, also had a friend help me but to no avail.我试过搜索,在代码中尝试了不同的方法,也有朋友帮助我,但无济于事。 My problem is that an array that I get from props is empty after setting some other state.我的问题是,在设置了其他一些 state 后,我从 props 获得的数组是空的。

I have a carousel component.我有一个轮播组件。 This component has two children:该组件有两个子组件:

Gallery (displays the images, either as cards, horizontaly or as a ul, stackad vertically) and Pagination (which is two buttons that return on click events and recieve booleans to be disabled).图库(将图像显示为卡片,水平或垂直堆叠)和分页(这是两个按钮,返回点击事件并接收要禁用的布尔值)。

Carousel gets an array called 'puffItems'. Carousel 得到一个名为“puffItems”的数组。 There are 11 items in this array.此数组中有 11 个项目。 When console logging both in the Carousel and in Gallery (which recieves the array) the array is first '0', then rerenders and changes to '11'.当控制台在轮播和画廊(接收数组)中登录时,数组首先是“0”,然后重新渲染并更改为“11”。 After clicking "Next" Carousel rerenders and now the array rerenders only one time and its '0'.单击“下一步”后,轮播重新渲染,现在数组仅重新渲染一次,其为“0”。 The funny thing is this only happens in mobile view.有趣的是,这只发生在移动视图中。 It works as expected in desktop view.它在桌面视图中按预期工作。

This is the code:这是代码:

Carousel:轮播:

const Carousel = ({
forwardButtonText,
backwardButtonText,
puffItems,}: Props) => {
const widthOfOnePuff = (px(gridMaxWidth - 16 * 2) + Theme.spacing.space2) / 3;

const [cssState, setcssState] = useState({
    translate: 0,
    transition: 0.40,
});

const isPhone = useMediaQuery({
    minWidth: breakpointsNumber.phone,
    maxWidth: breakpointsNumber.tablet - 1,
});

const { translate, transition } = cssState;
const [currentIndex, setCurrentIndex] = useState<number>(3);
const [slideWidth, setSlideWidth] = useState<number>(widthOfOnePuff * 3);
const [numberOfPuffsToSlide, setNumberOfPuffsToSlide] = useState<number>(3);

useEffect(() => {
    if (isPhone) {
        setSlideWidth(widthOfOnePuff);
    }
}, [isPhone]);

console.log('puffItems', puffItems);


const onPreviousSlide = () => {
    setcssState({
        ...cssState,
        translate: translate - slideWidth,
    });

    setCurrentIndex(prev => prev - numberOfPuffsToSlide);
};

const onNextSlide = () => {
    setcssState({
        ...cssState,
        translate: translate + slideWidth,
    });

    setCurrentIndex(prev => prev + numberOfPuffsToSlide);
};

return (
    <CarouselContainer>
        <Gallery
            puffItems={puffItems}
            translate={translate}
            transition={transition}
            isPhone={isPhone}
        />
        <Space top={Theme.spacing.space3}>
            <Pagination
                forwardButtonText={forwardButtonText}
                backwardButtonText={backwardButtonText}
                onPreviousSlide={onPreviousSlide}
                onNextSlide={onNextSlide}
                inactivateForwardButton={puffItems.length <= currentIndex}
                inactivateBackwardButton={currentIndex === 3}
            />
        </Space>
    </CarouselContainer>
);};export default Carousel;

Gallery:画廊:

const Gallery = ({
puffItems,
translate,
transition,
isPhone,}: GalleryProps) => {
const listLength = puffItems.length;
let numberOfColumns = 0;
if (isPhone) {
    numberOfColumns = listLength % 3 === 0
    ? Math.floor(listLength / 3)
    : Math.floor(listLength / 3) + 1;
}

console.log('puffItems gallery ', puffItems.length);

return isPhone ? (
    <div>
        <GalleryListStyle
            columns={numberOfColumns}
            myTranslate={translate}
            transition={transition}>

            <GalleryListItems
                puffItems={puffItems}
                columns={numberOfColumns}
            ></GalleryListItems>

        </GalleryListStyle>
    </div>
) : (
    <GalleryCardStyle myTranslate={translate} transition={transition}>
        {puffItems.map((puffItem, index) => (
            <EpiFragments fragments={[puffItem]} key={index} />
        ))}
    </GalleryCardStyle>
);};export default Gallery;

and finally, Pagination:最后,分页:

const Pagination = ({
forwardButtonText,
backwardButtonText,
onPreviousSlide,
onNextSlide,
inactivateForwardButton,
inactivateBackwardButton,}: Props) => {
return (
    <PaginationContainer>
        <Button
            variant={ButtonVariant.Tertiary}
            onClick={onPreviousSlide}
            disabled={inactivateBackwardButton}
        >
            {backwardButtonText}
        </Button>

        <Button
            variant={ButtonVariant.Tertiary}
            onClick={onNextSlide}
            disabled={inactivateForwardButton}
        >
            {forwardButtonText}
        </Button>
    </PaginationContainer>
);};export default Pagination;

In the image below you can see, that number 1,2 and 3 are the after page-refresh and number 4 is when I click 'Next', meaning 'onNextSlide' is prompted.在下图中,您可以看到,数字 1,2 和 3 是页面刷新后,数字 4 是当我单击“下一步”时,即提示“onNextSlide”。 在此处输入图像描述 Does anyone know why this is happening?有谁知道为什么会这样? And why only in mobile view (chrome developer tool, ~375px width)?为什么只在移动视图中(chrome 开发者工具,~375px 宽度)?

I do a 'splice' on the array further down in 'GalleryListItems', which alters the array.. changed it to slice.我在“GalleryListItems”中对数组进行了“拼接”,这会改变数组..将其更改为切片。 This solves the problem.这解决了问题。 Thanks anyways!不管怎么说,多谢拉!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM