2017-08-30 20 views
0

我想在React中使用Material-UI中的列表,其中列表還包含嵌套項目。我有這樣的代碼:提供給`ListItem`的`array`類型的nestedItems [0]預期只有一個ReactElement

<List> 
    {this.state.categories.map(category => { 
     return (
      <ListItem key={category.categoryID} 
         primaryText={category.name} 
         nestedItems={[ 
          category.subcategories.map(subcat => { 
           return (
            <ListItem key={subcat.subcatID} 
              primaryText={subcat.name} /> 
          ) 
          }) 
         ]} /> 
     ) 
    })} 
</List> 

問題是,雖然此代碼的作品,我看到一切顯示,我在控制檯中得到一個錯誤。我得到的錯誤與標題中的錯誤相同。我認爲問題在於nestedItem中的每一個ListItem都需要用逗號分隔,但是如果我在代碼中最後一個ListItem後面加逗號,就會出現語法錯誤。任何想法如何解決這個問題?

回答

1

由於地圖返回一個數組,有一個多餘的數組:

<ListItem key={category.categoryID} 
       primaryText={category.name} 
       nestedItems={ //here no need an array. This will be nestedItems=[[]] 
        category.subcategories.map(subcat => { 
         return (
          <ListItem key={subcat.subcatID} 
            primaryText={subcat.name} /> 
        ) 
        }) 
       } /> 
相關問題