首页 > 解决方案 > react native 中自定义组件的简写?

问题描述

我最近了解到您可以制作这样的自定义组件:

const CustomComp=()=>{
 console.log('Created custom comp');
 return(<View></View>);
}

export default function App(){
 return(<View style={{flex:1}}>
  <CustomComp />
 </View>)
}

但是有可能以速记方式做到这一点,也许是这样的?

export default function App(){
 return(<View style={{flex:1}}>
  <(()=>{
     console.log('Created custom comp');
     return(<View></View>);
    }) />
 </View>)
}

这不准确,但我想您对我的查询有所了解

标签: javascriptreactjsreact-native

解决方案


您可以使用 IIFE 就地评估函数:

export default function App(){
 return(<View style={{flex:1}}>
  {(() => {
     console.log('Created custom comp');
     return(<View></View>);
  })()}
 </View>)
}

推荐阅读