React-Native - 無法讀取未定義的屬性“cloneWithRowsAndSections”

[英]React-Native - Cannot read property “cloneWithRowsAndSections” of undefined


I'm doing a simple ListView with section headers a la this tutorial:

我在本教程中使用節標題做一個簡單的ListView:

https://medium.com/@darylrowland/reactnative-listview-with-section-headers-99a6714282c3#.627c88ikm

https://medium.com/@darylrowland/reactnative-listview-with-section-headers-99a6714282c3#.627c88ikm

and I'm stuck for days on the part when the ListView uses its datasource.

當ListView使用其數據源時,我已經停留了好幾天。

The class's constructor and the json object which is the source for the data:

類的構造函數和作為數據源的json對象:

    export class ProductListScreen extends Component {
  constructor(props) {
    super(props)

var ds = new ListView.DataSource({

  rowHasChanged: (r1, r2) => r1 !== r2,
  sectionHeaderHasChanged: (s1, s2) => s1 !== s2
})
    this.state = {
      location: {
          products: [
            {
              name: "Paprika baburaw",
              class: "3",
              price: "20",
              measure_unit: "7",
            },
            {
              name: "Paprika babura",
              class: "3",
              price: "20",
              measure_unit: "7",
            },
            {
              name: "Paprika  2",
              class: "3",
              price: "20",
              measure_unit: "7",
            },
            {
              name: "meso 2",
              class: "1",
              price: "20",
              measure_unit: "7",
            },
            {
              name: "meso 2",
              class: "1",
              price: "20",
              measure_unit: "7",
            },
            {
              name: "sir",
              class: "4",
              price: "20",
              measure_unit: "7",
            },
          ],
          vendor: {
                name: "OPG xy",
                adress: "Mije Stuparica 4",
                e_mail: "wololo@gmail.com",
                telephone: "0922423425",
          }
        },
      }

  }

My ListView inside the render is defined like this (takes sortProducts() as datasource):

渲染中的我的ListView定義如下(將sortProducts()作為數據源):

          <ListView
      dataSource={this.sortProducts()}
      renderRow={this.renderRow}
      renderSectionHeader={this.renderSectionHeader}
      renderSeparator={(sectionID, rowID) => <View key={rowID}/>}/>

And the render methods fo the rows and sections are also functional.

行和部分的渲染方法也是有用的。

The function:

功能:

  sortProducts() {
    var ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2,
      sectionHeaderHasChanged: (s1, s2) => s1 !== s2
    })
    var productClassMap = {}
      this.state.location.products.forEach( function (productItem) {
        if(!productClassMap[productItem.class]) {
          productClassMap[productItem.class] = []
        }
      productClassMap[productItem.class].push(productItem)
    })
  this.setState({
    dataSource: this.state.ds.cloneWithRowsAndSections(productClassMap)
  })
  return productClassMap
  }

So the function tumbles the object and makes the class element of the product the object's key which should serve as SectionHeader (1, 3 and 4 in this object).

因此,該函數會使對象翻轉,並使產品的類元素成為對象的鍵,該鍵應作為SectionHeader(此對象中的1,3和4)。

The problem lies when i try to set the state at the end of a function, and gives the error in this question's title.

問題在於我嘗試在函數結束時設置狀態,並在此問題的標題中給出錯誤。

        dataSource: this.state.ds.cloneWithRowsAndSections(productClassMap)

I'm guessing the error doesn't have much in common with the rest of the ListView, but I've put everything here so the context is clear.

我猜這個錯誤與ListView的其余部分沒有太大的共同之處,但我把所有內容都放在這里,所以上下文清晰。

What am I doing wrong? I'm banging my head for days.. Any pointers and suggestions are most appreciated.

我究竟做錯了什么?我好幾天都在敲我的頭..任何指針和建議都非常感激。

1 个解决方案

#1


2  

You are not initialization ds in state anywhere, thus it is undefined when you use it.

您不是在任何地方初始化ds,因此在使用它時它是未定義的。

In your sortProducts(), change dataSource: this.state.ds.cloneWithRowsAndSections(productClassMap) to dataSource: ds.cloneWithRowsAndSections(productClassMap)

在sortProducts()中,將dataSource:this.state.ds.cloneWithRowsAndSections(productClassMap)更改為dataSource:ds.cloneWithRowsAndSections(productClassMap)

As the variable ds is initialized before in function, this should work fine.

由於變量ds在函數之前被初始化,因此應該可以正常工作。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/12/09/8f266dbf5ad7698c454bb0f07fa297ec.html



 
  © 2014-2022 ITdaan.com