Home > database >  shorthand for custom components in react native?
shorthand for custom components in react native?

Time:10-22

I've recently learnt that you can make a custom component like this:

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

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

but is it possible to do it in shorthand maybe something like this?

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

it's not accurate but I guess you get the general idea of my query

CodePudding user response:

You can just evaluate a function in place using an IIFE:

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