【问题标题】:React Native passing data in auth fuctionReact Native 在 auth 函数中传递数据
【发布时间】:2020-07-27 03:15:36
【问题描述】:

我是 react native 的新手,我尝试将登录类中的数据传递给 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 constants


    【解决方案1】:

    你的状态更新了吗?

    <TextInput
      value={this.state.username}
      placeholder="Username"
      onChangeText={(value) => this.setState({ username: value })
    />
    

    【讨论】:

    • 我试过了,但现在每次调用“setState”时我都会失去对 TextInputs 的关注。
    猜你喜欢
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2017-12-19
    • 2018-03-13
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多