unable to access component with ref

可紊 提交于 2019-12-11 17:32:43

问题


Why is this.workmin.value undefined?

captureInput=(text)=>{
    console.log('captureinput',this.refs.workmin.value)
  }

  render() {
    console.log('RENDER',this.state)


    return (
      <View style={styles.container}>
        <Text style={styles.bigFont}>{`${this.state.timer + 'TIMER'}`}</Text>
        <Text style={styles.bigFont}>{`${this.state.min + ':' + this.state.sec}`}</Text>
        <View style={styles.button}>
          <Button title='START' onPress={()=>this.startToggle()} />
          <Button title='RESET' onPress={()=>this.resetToggle()} />
        </View>
             <View style={styles.row}>
                <Text style={[styles.bold,{marginRight:10},{width:112},
                            {textAlign:'right'}]}>
                            Work Timer:</Text>
                <Text style={styles.bold}> min:</Text>
                <TextInput
                   
                   value={Math.floor(this.state.workTime / 60).toString()}
                   ref= {(ref)=>{this.workmin=ref}}
                   style={styles.input}
                   onChangeText={(text) => {this.captureInput(text)}}

                />
      </View>
    )
  }

回答1:


refs is deprecated, do this instead:

<TextInput
  ref={(ref) => {
    this.workmin = ref;
  }}
/>

Then you can use it like this.workmin.focus().



来源:https://stackoverflow.com/questions/50090496/unable-to-access-component-with-ref

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!