简体   繁体   中英

Curly braces in react native

In react native render function, I've tried writing something like:

render(){
  return (
    <View>
      {let a=5}
    </View>
  )
}

I thought I can write there any JS code, but it seems that it's an error. Can anyone explain why?

The problem is not related to ReactNative, but to JSX syntax.

You can only embed expressions in JSX . let a=5 is not an expression but an assignment, try to execute (let a = 5) in your browser console, you will see an error. You have to declare your variables above your JSX.

render(){
   let a = 5;
   return (
     <View>
      {/* use `a` here */}
     </View>
   )
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM