-3

我正在尝试仅在 acollections存在时渲染标题,而在它为空时不渲染它。一个简单的修复,但我一直收到这个 Unexpected Token 语法错误,我似乎找不到问题。任何帮助将不胜感激,因为这是我第一次发布和使用 react-native!

const collectionsHeaderHandler = ({ favorites }) => 
  {favorites.meta.collections} ? 
  {copyDek.FavoritesLayout.collectionsHeader} : 
  "";

const FavoritesLayoutCollections = ({ favorites, onPressHandlers }) => (
  <FavoritesLayoutCollectionsWrapper>
    <FavoritesLayoutCollectionsHeader>
      {collectionsHeaderHandler}
    </FavoritesLayoutCollectionsHeader> 
    <FavoritesCollections
      collections={favorites.meta.collections}
      onPressCollection={onPressHandlers.onPressCollection}
    />
  </FavoritesLayoutCollectionsWrapper>
);
4

1 回答 1

0

您首先需要删除三元运算符中的括号来验证您的 js:

const collectionsHeaderHandler = ({ favorites }) =>
favorites.meta.collections ?
copyDek.FavoritesLayout.collectionsHeader :
"";

编辑以减少 linter 错误

const FavoritesLayoutCollections = ({ favorites, onPressHandlers }) => (
 <FavoritesLayoutCollectionsWrapper>
   <FavoritesLayoutCollectionsHeader>
     {collectionsHeaderHandler({favorites})}
   </FavoritesLayoutCollectionsHeader> 
   <FavoritesCollections
     collections={favorites.meta.collections}
     onPressCollection={onPressHandlers.onPressCollection}
   />
 </FavoritesLayoutCollectionsWrapper>
);
于 2018-04-03T19:17:04.010 回答