eiriklv / react-masonry-component

A React.js component for using @desandro's Masonry
MIT License
1.44k stars 145 forks source link

hide items until layout is ready? #97

Open freder opened 6 years ago

freder commented 6 years ago

hi,

I want to make sure the layout is ready, before I display all items. I tried the following, but that does not seem to do the trick (I still see a mess of items, for a fraction of a second):

import React from 'react';
import Masonry from 'react-masonry-component';

const masonryOptions = {
    transitionDuration: 0,
};

const MasonryLayout = React.createClass({
    getInitialState() {
        return { layoutReady: false };
    },

    handleLayoutReady() {
        if (!this.state.layoutReady) {
            this.setState({ layoutReady: true });
        }
    },

    render() {
        const { children } = this.props;

        return <div>
            <div
                className='wrapper'
                style={{ 
                    // display: (this.state.layoutReady)
                    //  ? undefined
                    //  : 'none',
                    visibility: (this.state.layoutReady)
                        ? 'visible'
                        : 'hidden', 
                }}
            >
                <Masonry
                    options={masonryOptions}
                    onLayoutComplete={this.handleLayoutReady}
                >
                    {children}
                </Masonry>
            </div>
        </div>;
    },
});

export default MasonryLayout;

am I missing anything?

sanmaopep commented 6 years ago

Try onLayoutComplete={this.handleLayoutReady.bind(this)} .

cdtinney commented 5 years ago

Try onLayoutComplete={this.handleLayoutReady.bind(this)} .

This should be done in the constructor otherwise this will create a new function every time render() is called.

e.g.

constructor(props) {
  super(props);

  this.handleLayoutReady = this.handleLayoutReady.bind(this);
}