javascript - 无法在 ReactNative 文本框中输入文本

标签 javascript html react-native input textbox

已编辑 我是 ReactNative 的新手,我正在尝试在我的文本框上书写。但是,我无法输入任何内容。我尝试将 redux 表单添加到注册屏幕。这是我的 Signup.js :

import React, { Component } from 'react';
import {StyleSheet, Text, View, StatusBar, TouchableOpacity} from 'react-native';
import {Field, reduxForm} from 'redux-form';

import Logo from '../components/Logo';
import Form from '../components/Form';
import InputText from '../components/InputText';

import {Actions} from 'react-native-router-flux';

const styles = StyleSheet.create({
    container:{
        backgroundColor: '#232122',
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center'
},
signupTextCont:{
    flexGrow: 1,
    alignItems:'flex-end',
    justifyContent: 'center',
    paddingVertical:16,
    flexDirection:'row'
},
signupText: {
 color:'rgba(255,255,255,0.7)',
 fontSize:16
},
signupButton: {
 color: '#FFA200',
 fontSize:16,
 fontWeight:'500'
},
button:{
 width:300,
 backgroundColor:'#FFA200',
 borderRadius:25,
 marginVertical:10,
 paddingVertical: 13
},
buttonText:{
 fontSize:16,
 fontWeight:'500',
 color:'#ffffff',
 textAlign:'center'
},
errorText:{
    color:"#FFA200",
    fontSize:14,
    paddingHorizontal:16,
    paddingBottom: 8
}
});

class Signup extends Component<{}> {

goBack() {
    Actions.pop();
}

createNewUser = () =>{
    alert("Utilizador criado com sucesso!")
}

onSubmit = (values)  => {
    console.log(values);
}

renderTextInput = (field) => {
const {meta: {touched, error}, label, secureTextEntry, maxLength, keyboardType, placeholder, input: {onChange, ...restInput}} = field;
return(
    <View>
        <InputText
            onChangeText={onChange}
            maxLength={maxLength}
            placeholder={placeholder}
            keyboardType={keyboardType}
            secureTextEntry={secureTextEntry}
            label={label}
            {...restInput} />
            {touched && <Text style={styles.errorText}>{error}</Text>}
        </View>
);
}


render() {
    const {handleSubmit} = this.props;
    return(
        <View style={styles.container}>
            <Logo/>
            <Field 
                name="name" 
                placeholder="Insira o seu nome"
                component={this.renderTextInput}/>
            <Field 
                name="email" 
                placeholder="Insira o seu email"    
                component={this.renderTextInput}/>
            <Field 
                name="password" 
                placeholder="Password"
                secureTextEntry={true}
                component={this.renderTextInput}/>
            <TouchableOpacity style={styles.button} onPress={handleSubmit(this.onSubmit)}>
             <Text style={styles.buttonText}>Signup</Text>
            </TouchableOpacity>
            <View style={styles.signupTextCont}>
                <Text style={styles.signupText}>Ja tens uma conta?</Text>
                <TouchableOpacity onPress={this.goBack}><Text style={styles.signupButton}> Entra</Text></TouchableOpacity>
            </View>
        </View>
        )
    }
}
const validate = (values) => {
const errors = {};
if(values.name){
    errors.name = "Name is required"
}
if(values.email){
    errors.email = "Email is required"
}
if(values.name){
    errors.password = "Password is required"
}
return errors;

}

export default reduxForm({
form: "register",
validate
})(Signup)

这是我的InputText:

import PropTypes from "prop-types";
import React, {Component} from "react"; 
import {TextInput, Text, View, StyleSheet} from "react-native";

const propTypes = {
mapElement: PropTypes.func,
onSubmitEditing: PropTypes.func,
onChangeText: PropTypes.func,
value: PropTypes.string,
placeholder: PropTypes.string,
maxLength: PropTypes.number,
keyboardType: PropTypes.string,
secureTextEntry: PropTypes.bool,
label: PropTypes.string
};

const defaultProps = {
mapElement: (n) => {},
onSubmitEditing: () => {},
onChangeText: () => {},
value: "",
placeholder: "",
maxLength: 200,
keyboardType: "default",
secureTextEntry: false,
label: ""
 };

const styles = StyleSheet.create({
  inputBox: {
width:300,
backgroundColor:'rgba(255, 255,255,0.2)',
borderRadius: 25,
paddingHorizontal:16,
fontSize:16,
color:'#ffffff',
marginVertical: 10
}

});

class InputText extends Component <{}> {
render() {
    const {placeholder, secureTextEntry, keyboardType, maxLength, value, onChangeText, onSubmitEditing} = this.props;
    return (
        <View>
            <TextInput
                style={styles.inputBox}
                underlineColorAndroid="rgba(0,0,0,0)"
                placeholder={placeholder}
                placeholderTextColor="rgba(255,255,255,0.8)"
                selectionColor="#999999"
                secureTextEntry={secureTextEntry}
                keyboardType={keyboardType}
                maxLength={maxLength}
                returnKeyType="next"
                value={value}
                onSubmitEditing={onSubmitEditing}
                onChangeText={this.onChangeText} />
        </View>
    );
}
}

InputText.defaultProps = defaultProps;

InputText.propTypes = propTypes;

export default InputText;

我不明白为什么会这样。请帮助我

最佳答案

<TextInput
  style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
  onChangeText={text => onChangeText(text)}
  value={value}
/>

我想试试这个方法

关于javascript - 无法在 ReactNative 文本框中输入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59634672/

相关文章:

javascript - chrome 内容脚本中未触发窗口 onload 事件

html - 将可滚动表格列与CSS中的标题对齐

javascript - ReactNative Flatlist - RenderItem 不工作

javascript - 将附加参数传递给 Node js 中 fs.readFile 的回调

php - 有没有办法将 PHP 值包含在 javascript 函数中?

javascript - 允许某些特殊字符,但必须是字母数字

jquery - 通过 jQuery 居中 DIV

html - 宽度 100% 不会用填充包裹在父级内?

javascript - 是否可以在 React Native 中使用 Animated 为 Modal 设置动画?

javascript - 在 react-native 中处理首次使用的用户