Undefined不是TextInput onSubmitEditing Focus的对象

时间:2019-08-27 09:44:00

标签: android ios react-native input

我试图在用户按下键盘上的“下一个”按钮时聚焦下一个字段。但我不断得到undefined is not an object (_this2.passwordInput.focus).

我指的是this问题来实施。而且没有用。

我有CustomInputText,这就是我实现它的方式。

<CustomEditText
    labelText="Email or Phone Number"
    returnKeyType = "next"
    onSubmitEditing={ () => this.passwordInput.focus() }
    onChangeText={text => this.setState({ username: text })}
    blurOnSubmit={false}
    />

<CustomEditText
    ref={(ref) => { this.passwordInput = ref; }}
    labelText="Password"
    secureText={true}
    onChangeText={text => this.setState({ password: text })}
    />

CustomEditText.js

const CustomEditText = props => {
  return (
    <View style={styles.container}>
      <SmallText fontFamily="semi-bold" color={props.darkText? Colors.TEXT_PRIMARY_COLOR : "#fff"}>
        {props.labelText}
      </SmallText>

      <TextInput
        ref = {props.ref}
        blurOnSubmit= {props.blurOnSubmit}
        onSubmitEditing = {props.onSubmitEditing}
        returnKeyType = {props.returnKeyType}
        onFocus = {props.onFocus}
        autoCapitalize="none"
        editable={props.editable}
        onChangeText={props.onChangeText}
        keyboardType={props.keyboardType}
        {...props}
        value={props.value}
      />
      {props.secondaryLabelText ? (
        <ExtraSmallText
          fontFamily="semi-bold"
          color={props.darkText? Colors.TEXT_PRIMARY_COLOR : "#fff"}
          style={styles.secondaryLabelStyle}
        >
          {props.secondaryLabelText}
        </ExtraSmallText>
      ) : null}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    margin: 5
  },
  textInputStyle: {
    backgroundColor: "#fff",
    height: 45,
    borderRadius: 5,
    borderWidth: 1,
    padding: 5,
    marginTop: 5,
    color:"#000"
  },
  secondaryLabelStyle: {
    marginTop: 5
  }
});

CustomEditText.propTypes = {
  labelText: propTypes.string.isRequired,
  secondaryLabelText: propTypes.string,
  keyboardType: propTypes.string,
  secureText: propTypes.bool,
  placeholderText: propTypes.string,
  placeholderTextColor: propTypes.bool,
  onChangeText: propTypes.func,
  darkText: propTypes.bool,
  editable: propTypes.bool,
  onFocus: propTypes.func,
  pointerEvents: propTypes.string,
  returnKeyType : propTypes.string,
  onSubmitEditing: propTypes.func,
  ref: propTypes.func,
  blurOnSubmit: propTypes.bool

};

export default CustomEditText;

1 个答案:

答案 0 :(得分:1)

使用以下代码更新您的CustomTextInput.js文件

<TextInput
        ref={input => props.inputRef && props.inputRef(input)}
        blurOnSubmit= {props.blurOnSubmit}
        onSubmitEditing = {props.onSubmitEditing}
        returnKeyType = {props.returnKeyType}
        onFocus = {props.onFocus}
        autoCapitalize="none"
        editable={props.editable}
        onChangeText={props.onChangeText}
        keyboardType={props.keyboardType}
        {...props}
        value={props.value}
      />

并像这样使用它

<CustomEditText
          inputRef={(ref) => this.passwordInput = ref}
          labelText="Password"
          secureText={true}
          // onChangeText={text => this.setState({ password: text })}
          />