代码之家  ›  专栏  ›  技术社区  ›  GuillaumeRZ

在onFocus()上只触发一个textInput

  •  0
  • GuillaumeRZ  · 技术社区  · 7 年前

    我的控制台.log(“触发”)每次我聚焦或编辑它,不管。

        <TextInput
          placeholder="firstname"
          textContentType="name"
          selectTextOnFocus
          onChangeText={value => this._handleStateEdition("firstname", value)}
        />
        <TextInput
          placeholder="lastname"
          textContentType="familyName"
          selectTextOnFocus
          onFocus={console.log("triggered")}
          onChangeText={value => this._handleStateEdition("lastname", value)}
        />
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Ammar Tariq    7 年前

    它们已经被隔离了,但是您可以添加autofocus=false,这样您的字段就不会自动聚焦,另外还有一个我从未使用过的方法是使用refs。

    自动对焦法

    <TextInput
      placeholder="firstname"
      textContentType="name"
      selectTextOnFocus
      autoFocus={false}
      onChangeText={value => this._handleStateEdition("firstname", value)}
    />
    <TextInput
      placeholder="lastname"
      textContentType="familyName"
      selectTextOnFocus
      autoFocus={false}
      onFocus={console.log("triggered")}
      onChangeText={value => this._handleStateEdition("lastname", value)}
    />
    

    <TextInput
      placeholder="firstname"
      textContentType="name"
      selectTextOnFocus
      autoFocus={false}
      ref={r=>this.first = r}
      onChangeText={value => this._handleStateEdition("firstname", value)}
    />
    <TextInput
      placeholder="lastname"
      textContentType="familyName"
      selectTextOnFocus
      autoFocus={false}
      onFocus={console.log("triggered")}
      onChangeText={value => this._handleStateEdition("lastname", value)}
      ref={r=>this.last = r}
    />
    

    componentDidMount(){
    this.first.onFocus = ()=>{
     //some function
    }
    this.last.onFocus = ()=>{
     //some function
    }
    }
    
    推荐文章