简体   繁体   中英

When to surround props with curly braces in React Native?

In the official tutorial, this code shows up:

    <TextInput
      style={{height: 40}}
      placeholder="Type here to translate!"
      onChangeText={(text) => this.setState({text})}
    />

Why do we surround height with curly braces but not placeholder ?

You're surrounding height with curly braces because you're passing javascript to the style and onChangeText props. Strings can be passed literally where as javascript needs to be surrounded by curly braces. It's part of the JSX syntax

Height是JavaScript对象的属性,该对象将传递给option占位符是TextView组件的选项。

placeholder="Type here to translate!"

and

placeholder={'Type here to translate!'}

are equivalent. You can also write it like this, to visually separate it better:

placeholder={
  'Type here to translate!'
}

Knowing that, you can pass almost anything to react props, like an object:

style={
  {height: 40}
}

Which, written inline, results in what you encountered:

style={{height: 40}}

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