React Native 如何从TextInput获取值

sqougxex  于 2023-04-12  发布在  React
关注(0)|答案(4)|浏览(201)

我被困在一个看起来很简单的函数中。我如何从TextInput中获得一个值(字符串)?
下面是代码的摘录:

const Insert = props => {
  const [enteredName, setEnteredName] = useState();

const sendValues = (enteredName) => {

    console.log(enteredName);

  };

 <TextInput
          placeholder="Your Name"
          blurOnSubmit
          autoCorrect={false}
          maxLength={30}
          autoCapitalized="words"
          placeholderTextColor="#777"
          value={enteredName}
          onChangeText={text => setEnteredSurname(text)}

        />

        <View>
          <Button title="Submit" onPress={sendValues(enteredName)} />

我得到的打字时,我键入,但它不提交任何东西。
“怎么样?
先谢谢你了!

iovurdzv

iovurdzv1#

您应该将onPress从表达式转换为函数并初始化您的状态

const Insert = props => {
  const [enteredName, setEnteredName] = useState(''); //INIT TO EMPTY

function sendValues(enteredName) {
    console.log(enteredName);
};

 <TextInput
    placeholder="Your Name"
    blurOnSubmit
    autoCorrect={false}
    maxLength={30}
    autoCapitalized="words"
    placeholderTextColor="#777"
    value={enteredName}
    onChangeText={text => setEnteredSurname(text)} />

    <View>
      <Button title="Submit" onPress={() => sendValues(enteredName)} /> //Function not expression
    </View>
js81xvg6

js81xvg62#

我遇到了同样的问题,我刚开始学习react native,下面的解决方案用于从TextInput组件获取值

const [input, setInput] = useState(''); 

<TextInput 
style={ styles.textinput }
placeholder='type somthing'
onChange={ (text) => {
   console.log(text.nativeEvent.text)
   setInput(text.nativeEvent.text)
      }
   }

defaultValue={ input }
/>
eqfvzcg8

eqfvzcg83#

class AwesomeProject extends Component {

  constructor(props){
    super(props)

    this.state = {
      username: '',
      password: '',
    }
  }

  _handlePress() {
     console.log(this.state.username);
     console.log(this.state.password);
  }

  render() {
    return (
    <ScrollView style={styles.content}>
      <View style={styles.content}>

        <Text style={styles.welcome}>
          Create Account
        </Text>

        <Text style={styles.textStyle}>
          Name
        </Text>

        <TextInput
          style={styles.textInputStyle}
          placeholder="Enter Name"
          returnKeyLabel = {"next"}
          onChangeText={(text) => this.setState({username:text})}
        />

        <Text style={styles.textStyle}>
          Name
        </Text>

        <TextInput
          style={styles.textInputStyle}
          placeholder="Enter Name"
          returnKeyLabel = {"next"}
          onChangeText={(text) => this.setState({password:text})}
        />

        <Button 
          onPress={() => this._handlePress()}
          style={styles.buttonStyle}>
              Submit
        </Button>

        </View>
      </ScrollView>
    );
  }
}
oewdyzsn

oewdyzsn4#

试试这个,你必须这样使用它:

import React, { useState } from 'react';
import {Text, View,TextInput,Button} from 'react-native';
export default  Example = () =>  {

   const [enteredName, setEnteredName] = useState(''); 
  sendValues = (enteredName) =>{
    console.log(enteredName);
};
  return (
    <View>
    <Text>Hey</Text>
    <TextInput
    placeholder="Your Name"
    blurOnSubmit
    autoCorrect={false}
    maxLength={30}
    autoCapitalized="words"
    placeholderTextColor="#777"
    value={enteredName}
    onChangeText={text => setEnteredSurname(text)} />

    <View>
      <Button title="Submit" onPress={() => sendValues(enteredName)} />
    </View>
    </View>
  );
}

相关问题