React Native通过身份验证传递数据

问题描述 投票:0回答:1

im react in im new in 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>

我现在已经花了几天时间和谷歌搜索时间,但是我找不到解决方案。您能帮我吗?

最诚挚的问候。

javascript ios react-native parameter-passing const
1个回答
0
投票

您的状态是否正在更新?

<TextInput
  value={this.state.username}
  placeholder="Username"
  onChangeText={(value) => this.setState({ username: value })
/>
© www.soinside.com 2019 - 2024. All rights reserved.