React native 如何从TextInput获取值
我被困在一个显然很简单的函数中。 如何从TextInput中获取值(字符串) 下面是代码的摘录:React native 如何从TextInput获取值,react-native,textinput,react-native-textinput,React Native,Textinput,React Native Textinput,我被困在一个显然很简单的函数中。 如何从TextInput中获取值(字符串) 下面是代码的摘录: const Insert = props => { const [enteredName, setEnteredName] = useState(); const sendValues = (enteredName) => { console.log(enteredName); }; <TextInput placeholder="Y
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)} />
const Insert=props=>{
const[enteredName,setEnteredName]=useState();
const sendValues=(enteredName)=>{
console.log(输入名称);
};
SetEnteredLastname(文本)}
/>
当我打字时,我得到了输入,但它没有提交任何东西
你知道吗
提前谢谢 类AwesomeProject扩展组件{
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>
);
}
}
建造师(道具){
超级(道具)
此.state={
用户名:“”,
密码:“”,
}
}
_女佣(){
console.log(this.state.username);
console.log(this.state.password);
}
render(){
返回(
创建帐户
名称
this.setState({username:text})
/>
名称
this.setState({password:text})}
/>
这个。_handlePress()}
样式={style.buttonStyle}>
提交
);
}
}
您应该将onPress从表达式转换为函数,并初始化您的状态
const Insert=props=>{
const[enteredName,setEnteredName]=useState(“”);//初始化为空
函数sendValues(enteredName){
console.log(输入名称);
};
SetEnteredLastname(文本)}/>
sendValues(enteredName)}/>//函数不是表达式
试试这个,你必须像这样使用它:
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>
);
}
import React,{useState}来自“React”;
从“react native”导入{Text,View,TextInput,Button};
导出默认示例=()=>{
常量[enteredName,setEnteredName]=useState(“”);
sendValues=(enteredName)=>{
console.log(输入名称);
};
返回(
嘿
SetEnteredLastname(文本)}/>
sendValues(enteredName)}/>
);
}
谢谢,伙计,这很管用