我在React Native中是新的,我尝试将数据从登录类传递给auth函数,但是它不起作用。
这是我的Auth功能
function auth(username, password) {
fetch('http://192.168.178.26:8000/auth/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password })
})
.then(res => res.json())
.then(res => {
saveToken(res.token);
console.log(res.token);
props.navigation.navigate('Home');
})
.catch(error => console.log(error));
构造函数
constructor() {
super()
this.state = {
isReady: false,
username: '',
password: '',
}
this.auth = auth(this.state.username, this.state.password)
...more code
render() {
const {password} = this.state.password;
const {username} = this.state.username;
我的文本输入和登录按钮
<TextInput
placeholder='Email'
...
value={username}/>
<TextInput placeholder='Password'
...
value={password}/>
<TapGestureHandler>
<Animated.View style={style.button} >
<Text style={{ ... onPress={this.auth}>Anmelden</Text>
我现在已经花了几天时间和谷歌搜索时间,但是我找不到解决方案。你能帮我吗?
最诚挚的问候。
答案 0 :(得分:0)
您的状态是否正在更新?
<TextInput
value={this.state.username}
placeholder="Username"
onChangeText={(value) => this.setState({ username: value })
/>