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

Hacky fixing overlaps #63

Open
dimitar-nikovski opened this issue May 13, 2019 · 5 comments
Open

Hacky fixing overlaps #63

dimitar-nikovski opened this issue May 13, 2019 · 5 comments

Comments

@dimitar-nikovski
Copy link

I found that this works by updating the layout after 1 animation frame:

const grid = useRef(null);

  useEffect(() => {
    requestAnimationFrame(() => {
      if (grid.current) {
        grid.current.updateLayout();
      }
    });
  }, [grid]);

<StackGrid
      gridRef={r => (grid.current = r)}
    >{children}</StackGrid>
@berkayk
Copy link

berkayk commented May 16, 2019

I am not using hooks, is there any way that I can use this hack? I am currently using setState to set children to render but initial layout is overlapping. If I updateLayout 1 seconds later using setTimeout, it works.

@dimitar-nikovski
Copy link
Author

@berkayk if you are using setState this means and async update is scheduled, so your previous render doesn't have the children? It depends on where you are calling setState, I would recommend putting the data which the grid depends on into props either in its parent component or extract it into a thin SFC with the hook above, which contains only that and other props which go to StackGrid

@carlmagumpara
Copy link

carlmagumpara commented Jun 6, 2019

Im using this in children component and it works smoothly

https://github.com/maslianok/react-resize-detector

<StackGrid
  gridRef={grid => this.grid = grid} 
  columnWidth={width <= 768 ? '100%' : '33.33%'}
  gutterWidth={15}
  gutterHeight={15}
  monitorImagesLoaded={true}>         
  <ReactResizeDetector 
    handleWidth  
    handleHeight 
    onResize={() => {
      if (this.grid) {
        this.grid.updateLayout();
      }
    }}>
     <div>
        <img />
     </div>
  </ReactResizeDetector>
</StackGrid>

@kaipradel
Copy link

kaipradel commented Sep 26, 2019

Instead of the requestanimationframe hack, you can use useRef to store a class variable.

const firstUpdate = useRef(true);
 useEffect(() => {
        if (firstUpdate.current) {
            firstUpdate.current = false;
            return;
        }
        if (grid.current) {
            grid.current.updateLayout();
        }
       
    }, [element.width, element.stack.sizes]);

@lukesiedle
Copy link

That's very helpful. I tweaked the solution slightly to trigger grid.current.updateLayout each time an image's onLoad event was triggered, ensuring that delayed load times doesn't affect it. E.g.

  useEffect(() => {
    if( grid.current ){
      grid.current.updateLayout();
    }
  }, [imageLoadCounter])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants