React Native / ES6 - 意外的令牌,预期“;”错误

时间:2018-04-03 19:13:29

标签: reactjs react-native syntax ecmascript-6 unexpected-token

我正在尝试仅在collections存在时呈现标题,并在空白时不呈现标题。一个简单的修复,但我一直收到这个意外的令牌语法错误,我似乎无法找到问题。任何帮助将不胜感激,因为这是我第一次发布和使用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>
);

1 个答案:

答案 0 :(得分: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>
);