react native中使用axios做get请求和post请求

import React, { useState, useRef, useEffect } from 'react'
import { View, TextInput, Text, Button } from 'react-native'
import axios from 'axios'
import style from './static/style'

export default function App() {
  const [username, setUsername] = useState('admin')
  const [password, setPasswork] = useState('123456')
  const usernameEl = useRef(null)

  const handleInput = (e) => {
    console.log(e)
    setUsername(e)
  }

  const handleLogin = () => {
    console.log(777, username, password)
    axios({
      url: 'http://10.3.138.173:81/api/getUserInfo',
    }).then((res) => {
      console.log(res)
    })
    axios({
      url: 'http://10.3.138.173:81/api/login',
      method: 'post',
      data: { username, password },
    }).then((res) => {
      console.log(res)
    })
  }

  useEffect(() => {
    //usernameEl.current.focus()
    //console.log(666, usernameEl.current.isFocused())
  }, [])

  return (
    <View style={style.mLoginWrap}>
      <View style={style.mLoginRow}>
        <TextInput
          style={style.mLoginInput}
          value={username}
          onChangeText={handleInput}
          // autoFocus
          ref={usernameEl}
        ></TextInput>
      </View>
      <View style={style.mLoginRow}>
        <TextInput
          style={style.mLoginInput}
          value={password}
          onChangeText={setPasswork}
          secureTextEntry={true}
        ></TextInput>
      </View>
      <View style={style.mLoginRow}>
        <Button onPress={handleLogin} title="登录"></Button>
      </View>
    </View>
  )
}

 

posted @ 2022-04-02 15:17  徐同保  阅读(0)  评论(0编辑  收藏  举报  来源