【问题标题】:ReactNative TextInput not letting me typeReactNative TextInput 不让我输入
【发布时间】:2018-02-17 18:51:38
【问题描述】:

适用于 iOS 和 Android 模拟器

当我开始输入时,文本会消失/闪烁。我尝试让文本具有一些值的初始状态,而不是保持为空。有了这个,TextInput 就会保持这个初始状态,并且不会用输入的新文本进行自我更新。

我认为状态没有使用“onChangeText”属性进行更新,但我不完全确定。

人们似乎已经解决了这个问题,因为他们在代码中几乎没有错别字或缺失的部分。但是我已经彻底检查了我的。

如果我遗漏了以下代码中的任何内容,请提供帮助。

LoginForm.js

import React, { Component } from 'react';
import { Card, Button, CardSection, Input } from './common';

class LoginForm extends Component {

  state = { email: '', password: '' }

  render() {
    return (
      <Card>
        <CardSection>
          <Input
            label="Email"
            placeHolder="user@gmail.com"
            onChangeText={text => this.setState({ email: text })}
            value={this.state.email}
          />
        </CardSection>

        <CardSection>
          <Input
            secureTextEntry
            label="Password"
            placeHolder="password"
            onChangeText={text => this.setState({ password: text })}
            value={this.state.password}
          />
        </CardSection>

        <CardSection>
          <Button>
            Log In
          </Button>
        </CardSection>
      </Card>
    );
  }
}

export default LoginForm;

Input.js

import React from 'react';
import { TextInput, View, Text } from 'react-native';

const Input = ({ label, value, onChangeText, placeholder, secureTextEntry }) => {
  const { inputStyle, labelStyle, containerStyle } = styles;

  return (
    <View style={containerStyle}>
      <Text style={labelStyle}>{label}</Text>
      <TextInput
        secureTextEntry={secureTextEntry}
        placeholder={placeholder}
        autoCorrect={false}
        style={inputStyle}
        value={value}
        onChangeText={onChangeText}
      />
    </View>
  );
};

const styles = {
  inputStyle: {
    color: '#000',
    paddingRight: 5,
    paddingLeft: 5,
    fontSize: 18,
    lineHeight: 23,
    flex: 2
  },
  labelStyle: {
    fontSize: 18,
    paddingLeft: 20,
    flex: 1
  },
  containerStyle: {
    height: 40,
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center'
  }
};

export { Input };

【问题讨论】:

  • 不应该是onChangeText={e =&gt; this.setState({ password: e.target.value })}吗?
  • 抱歉刚刚注意到您使用的是来自react-nativeTextInput
  • @Sagivb.g 没问题。如果问题有任何误导性内容或标签,请告诉我。
  • 我会将onChangeText 提取到类方法并调试/记录更改,以确保您获得正确的值并确保问题在于状态未更新跨度>
  • 发现你的问题,你的Input组件写错了。我会用答案解释

标签: javascript reactjs react-native


【解决方案1】:

解决此问题的唯一方法是更改​​ TextInput 字段值的更新方式,代码如下。

value={this.state.email.value}
value={this.state.password.value}

【讨论】:

  • 为什么会这样?我曾经有value={this.state.email},然后添加了.value,它开始工作了。 react-native 是否将.value 添加到状态中的每个键或其他东西?我从来没有遇到过常规反应的问题
【解决方案2】:

你的问题是Input 组件是如何编写的。
有一个 render 函数写在无状态组件内部,它不是 React class 组件:

const Input = ({ label, value, onChangeText, placeHolder, secureTextEntry }) => ( // ← remove the wrapping parentheses 
  {
    render() { // <--- this should not be here
      ↑
      const { inputStyle, labelStyle, containerStyle } = styles;
      return (
        <View style={containerStyle} >
          <Text style={labelStyle}>{label}</Text>
          <TextInput
            secureTextEntry={secureTextEntry}
            autoCorrect={false}
            placeholder={placeHolder}
            style={inputStyle}
            onChangeText={onChangeText}
            value={value}
            underlineColorAndroid="transparent"
          />
        </View>
      );
    }

  }
);

改成这样:

const Input = ({ label, value, onChangeText, placeHolder, secureTextEntry }) => {
  const { inputStyle, labelStyle, containerStyle } = styles;
  return (
    <View style={containerStyle} >
      <Text style={labelStyle}>{label}</Text>
      <TextInput
        secureTextEntry={secureTextEntry}
        autoCorrect={false}
        placeholder={placeHolder}
        style={inputStyle}
        onChangeText={onChangeText}
        value={value}
        underlineColorAndroid="transparent"
      />
    </View>
  );
};

See running example

【讨论】:

  • 根据您的建议进行了更改,但这不起作用。问题还是一样的。感谢您的努力!
  • 奇怪,您的代码在代码沙箱中为我工作。我会尝试发布一个 stack-sn-p
  • 我找不到react-native cdn 文件,所以我发布了一个代码沙箱项目的链接。您可以看到您的代码和我所做的更改有什么不同(尽管我所做的所有更改都发布在我的答案中)
  • 在代码沙箱项目中看到了。感到很高兴,至少它在那里工作。很奇怪,我无法在我的项目中实现同样的目标。我确实用您的建议替换了我的代码。这很奇怪
  • 注意变化,去掉最外面的括号(),去掉render和它的花括号render{}。当我按原样运行您的代码时,这是唯一的问题
猜你喜欢
  • 2016-05-07
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
相关资源
最近更新 更多