Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

my cards always rerender #51

Open
Taha-Seddik opened this issue Apr 14, 2020 · 7 comments
Open

my cards always rerender #51

Taha-Seddik opened this issue Apr 14, 2020 · 7 comments
Labels
faq Add to readme

Comments

@Taha-Seddik
Copy link

Taha-Seddik commented Apr 14, 2020

After wrapping my cards with react-masonry-css the shouldComponentUpdate in my card component is ignored and my cards keeps rendering !
can someone help me how to solve this issue ?

 render() {
        // console.log("render CardsContainer")
        const {
            view,
            cells,
            memberAvatar,
            imageThumbnail,
            COLORS_CONFIG,
            zoomValue,
        } = this.props;


        return (
            <React.Fragment>
                <Masonry
                    breakpointCols={6}
                    className="my-masonry-grid"
                    columnClassName="my-masonry-grid_column">

                    {cells[0][0].map((item, itemIndex) => (
                        <CardComponent
                            zoomValue={zoomValue}
                            key={item.getEntity().getId()}
                            itemIndex={itemIndex}
                            item={item}
                            view={view}
                            imageThumbnailFilter={imageThumbnail}
                            memberAvatarFilter={memberAvatar}
                            colorConfig={
                                COLORS_CONFIG.OptionsBackgroundColorsByPallete[0]
                            }
                            colorConfigRGB={
                                COLORS_CONFIG.OptionsBackgroundColorsByPallete[1]
                            }
                        />
                    ))}

                </Masonry>

            </React.Fragment >

        );
    }

@zliangs
Copy link

zliangs commented May 30, 2020

I have the same issue. How to solve it? Thanks @paulcollett @Noobday @dandv

@Taha-Seddik
Copy link
Author

I figured out the cause of continuous mounting and unmounting

I was changing the number of columns of masonry layout and react was continuously constructing the layout with cards When a card moves from a column to another column it unmount and mount

for that i did put a constant number of columns and the problem was solved

@Taha-Seddik
Copy link
Author

Taha-Seddik commented May 30, 2020

I checked the code of library i found that some sort of transformation to the component childrens caused the rerender
below some code from this library source code


 itemsInColumns() {
        const currentColumnCount = this.state.columnCount;
        const itemsInColumns = new Array(currentColumnCount);

        // Force children to be handled as an array
        const items = [].concat(this.props.children|| []);

        for (let i = 0; i < items.length; i++) {
            const columnIndex = i % currentColumnCount;

            if (!itemsInColumns[columnIndex]) {
                itemsInColumns[columnIndex] = [];
            }

            itemsInColumns[columnIndex].push(items[i]);
        }

        return itemsInColumns;
    }

and in the render he is displaying the array

const childrenInColumns = this.itemsInColumns();
return childrenInColumns.map((items, i) => {
          return <div
            key={i}
          >
            {items}
          </div>; 

without this transformation everything works normally

@Taha-Seddik
Copy link
Author

hope u got an idea about why i got this issue and how i solved it also what causes the render hell

@zliangs
Copy link

zliangs commented May 30, 2020

thank you so much @Noobday

@paulcollett
Copy link
Owner

paulcollett commented May 31, 2020

to confirm, this re-render only occurs on a column count change?

If so, that's an expected trade-off due to this technique which requires wrapping each column in an extra <div>. Unfortunately React doesn't optimise for "moving" a Component or it's dom elements between a shared ancestor element - It simply un-mounts and re-mounts.

We've looked into various workarounds, like <Portals> without success.

If this affects your Component's state, we suggest lifting the state above Masonry.

@Taha-Seddik
Copy link
Author

yep that's it

@paulcollett paulcollett added the faq Add to readme label Aug 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
faq Add to readme
Projects
None yet
Development

No branches or pull requests

3 participants