paulcollett / react-masonry-css

React Masonry layout component powered by CSS, dependancy free
https://paulcollett.github.io/react-masonry-css/demo/
MIT License
962 stars 66 forks source link

updating the breakpointCols object doesn't update the app until i manually resize it #20

Open hardtekpt opened 6 years ago

hardtekpt commented 6 years ago

Having a sidebar in my app I would like the columns to update with the sidebar being open or not. To do this i updated the breakpointCols object with new values but it doesn't change the app until i manually change the window size. Not sure if I'm being clear or not, but hope the images bellow help.

captura de ecra 2018-08-31 as 15 43 14

As you can see here the columns are displaying correctly

captura de ecra 2018-08-31 as 15 46 39

However when I open the sidebar the breakpoint values get updated but not the app. In this case there should be only 2 columns

captura de ecra 2018-08-31 as 15 49 04

The desired behaviour only occurs when i resize the windows itself


I would really appreciate if someone could help me fix this

EDIT:

here's the code: (For whatever reason I can't format it here) I swapped the code for an image as I could not format it right

captura de ecra 2018-08-31 as 22 10 44
paulcollett commented 6 years ago

it's possible to trigger a refresh manually, by using a ref on the component and calling the reCalculate function.

Ideally when changing you specify new breakpoint col values we do update the layout accordingly. I'm going to look into making sure this piece works

hardtekpt commented 6 years ago

Thanks for helping!!

Can you explain this manual refresh pls. Maybe with an example. Also if you need any other parts of the code I can provide them

rotev commented 5 years ago

@paulcollett is referring to the reCalculateColumnCount method defined here: https://github.com/paulcollett/react-masonry-css/blob/85083b8143e6b25a1b083ba19d81e777bb64b4bc/src/react-masonry-css.js#L42

Implementing the above solution would look something like that:

class MyComponent extends React.Component {
  constructor() {
    super();
    this.masonryRef = React.createRef();
  }

  componentDidUpdate(prevProps, prevState, snapshot) {
    const masonry = this.masonryRef.current;
    masonry && masonry.reCalculateColumnCount();
  }

  render() {
    const { breakpointCols } = this.props;
    return <Masonry ref={ this.masonryRef } breakpointCols={ breakpointCols } />
  }
}