question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to define dynamic LayoutProvider

See original GitHub issue

I am trying to define layout provider where type is basically coming in data source, so how can i define layout provider to take layout type when data source is updated. Sample code of my requirement is: this.layoutProvider = new LayoutProvider( index => { return this.getDataSource()[index].widgetType }, (type, dim) => { dim.width = width; dim.height = height; } );

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
maxfarseercommented, May 28, 2018

@nehacurefit when you receive new Data, you can access it inside componentWillReceiveProps (or getDerivedPropsFromState). After - you can setup new layout provider, and after you can setup new data provider. Is it works for you?

const DataSourceFactory = () =>
  new DataProvider((r1, r2) => {
    return r1 !== r2;
  });

const LayoutProviderFactory = results => {
  return new LayoutProvider(
    index => {
      const searchItem = results[index];
      if (!searchItem) {
        return 'unknown';
      }
      return searchItem.layoutType;
    },
    (type: SearchItemLayout, dim) => {
      switch (type) {
        case SEARCH_ITEM_LAYOUT_TYPES.Cover:
          dim.width = SEARCH_SIDEBAR_WIDTH;
          dim.height = SEARCH_ITEM_COVER_HEIGHT;
          break;
        case SEARCH_ITEM_LAYOUT_TYPES.Match:
          dim.width = SEARCH_SIDEBAR_WIDTH;
          dim.height = SEARCH_ITEM_HEIGHT;
          break;
        default:
          dim.width = 0;
          dim.height = 0;
      }
    }
  );
};
...
class ResultList extends React.Component<Props, State> {
  _layoutProvider: LayoutProvider;
  _dataProvider: DataProvider;

  constructor(props: Props) {
    super(props);

    const searchResults = this.props.preparedSearchResults;
    this._dataProvider = DataSourceFactory();
    this._layoutProvider = LayoutProviderFactory(searchResults);

    this.state = {
      dataProvider: this._dataProvider.cloneWithRows(searchResults),
    };
  }
...
...
componentWillReceiveProps(nextProps) {
    if (this.props.preparedSearchResults !== nextProps.preparedSearchResults) {
      const searchResults = nextProps.preparedSearchResults;
      this._dataProvider = DataSourceFactory();
      this._layoutProvider = LayoutProviderFactory(searchResults);

      this.setState(prev => ({
        ...prev,
        dataProvider: this._dataProvider.cloneWithRows(searchResults),
      }));
    }
  }
...
render() {
    const {
      initialRenderIndex,
      preparedSearchResults,
    } = this.props;

    return (
      <View style={styles.root}>
        <RecyclerListView
          canChangeSize
          layoutProvider={this._layoutProvider}
          dataProvider={this.state.dataProvider}
          rowRenderer={this._rowRenderer}
          initialRenderIndex={initialRenderIndex}
        />
      </View>
    );
  }

@naqvitalha is it correct behaviour? (because I have some problems here, related to my previous bug with initialRenderIndex)

1reaction
maxfarseercommented, Jan 23, 2021

@jdpagley happy new 2021 👯

Read more comments on GitHub >

github_iconTop Results From Across the Web

Visualizing network graphs — Bokeh 2.4.3 Documentation
Bokeh comes with a built-in LayoutProvider model that includes a dictionary of (x,y) coordinates for nodes. This lets you arrange plot elements in...
Read more >
Build an image browsing App with React Native (4) - Medium
What we need to do is implementing our own DataProvider , with which the array of images are consumed, and LayoutProvider , with...
Read more >
How to use the recyclerlistview.LayoutProvider function ... - Snyk
To help you get started, we've selected a few recyclerlistview.LayoutProvider examples, based on popular ways it is used in public projects.
Read more >
how create a dynamic page in react typescript and json file
i have made an app that has a users page and each user has its own page. this is my code in the...
Read more >
Building a Dynamic Layout in React - YouTube
Learn how to create a re-usable list component in React that allows for rendering grid and list based data along with filtering data....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found