Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何让axios.post通过单击按钮更新我的API?(帮助)_Javascript_React Native_Api_Post - Fatal编程技术网

Javascript 如何让axios.post通过单击按钮更新我的API?(帮助)

Javascript 如何让axios.post通过单击按钮更新我的API?(帮助),javascript,react-native,api,post,Javascript,React Native,Api,Post,我的获取和发布Axios代码: import React, { useState, useEffect } from "react"; import { StyleSheet, Text, View, Image, ScrollView } from "react-native"; import Card from "../../molecules/Card"; import Button from "../../atoms/

我的获取和发布Axios代码:

import React, { useState, useEffect } from "react";
import { StyleSheet, Text, View, Image, ScrollView } from "react-native";
import Card from "../../molecules/Card";
import Button from "../../atoms/Button";
import Axios from "axios";

const HomeScreen = () => {
  const [users, setUsers] = useState([]);

  useEffect(() => {
    //Fetch
    // fetch('https://reqres.in/api/users')
    //   .then(res => res.json())
    //   .then(json => setUsers(json.data));

    //Axios
    Axios.get("http://localhost:3004/users").then((res) => setUsers(res.data));
  }, [users]);

  const handleSubmit = () => {
    const data = {
      email: "angelin.veronica@mail.com",
      first_name: "Angelin",
      last_name: "Veronica",
      avatar: "https://reqres.in/img/faces/7-image.jpg",
    };
    Axios.post("http://localhost:3004/users", data);
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}> Home Screen </Text>

      <ScrollView showsVerticalScrollIndicator={false}>
        <Button label="Update" onSubmit={handleSubmit} />
        {users.map((item) => (
          <Card
            key={item.id}
            fullName={`${item.first_name} ${item.last_name}`}
            // name={item.name}
            // username={item.username}
            email={item.email}
            imageUrl={item.avatar}
            // address={`${item.address.street}, ${item.address.suite}, ${item.address.city}, ${item.address.zipcode}`}
            // phone={item.phone}
          />
        ))}
      </ScrollView>
    </View>
  );
};

export default HomeScreen;
import React,{useState,useffect}来自“React”;
从“react native”导入{样式表、文本、视图、图像、滚动视图};
从“../../molecles/Card”导入卡片;
从“../../atoms/Button”导入按钮;
从“Axios”导入Axios;
常量主屏幕=()=>{
const[users,setUsers]=useState([]);
useffect(()=>{
//取回
//取('https://reqres.in/api/users')
//.then(res=>res.json())
//.then(json=>setUsers(json.data));
//Axios
Axios.get(“http://localhost:3004/users)然后((res)=>setUsers(res.data));
},[用户];
常量handleSubmit=()=>{
常数数据={
电子邮件:“安吉林。veronica@mail.com",
名字:“安吉林”,
姓:“Veronica”,
阿凡达:“https://reqres.in/img/faces/7-image.jpg",
};
Axios.post(“http://localhost:3004/users“,数据);
};
返回(
主屏幕
{users.map((项)=>(
))}
);
};
导出默认主屏幕;
我的API= [ { “id”:7, “电子邮件”:“迈克尔。lawson@reqres.in", “名字”:“迈克尔”, “姓氏”:“劳森”, “阿凡达”:https://reqres.in/img/faces/7-image.jpg" }, { “id”:8, “电子邮件”:“林赛。ferguson@reqres.in", “名字”:“林赛”, “姓”:“弗格森”, “阿凡达”:https://reqres.in/img/faces/8-image.jpg" }, { “id”:9, “电子邮件”:“托拜厄斯。funke@reqres.in", “名字”:“Tobias”, “姓”:“Funke”, “阿凡达”:https://reqres.in/img/faces/9-image.jpg" }, { “id”:10, “电子邮件”:“拜伦。fields@reqres.in", “名字”:“拜伦”, “姓氏”:“字段”, “阿凡达”:https://reqres.in/img/faces/10-image.jpg" }, { “id”:11, “电子邮件”:“乔治。edwards@reqres.in", “名字”:“乔治”, “姓”:“爱德华兹”, “阿凡达”:https://reqres.in/img/faces/11-image.jpg" }, { “id”:12, “电子邮件”:“瑞秋。howell@reqres.in", “名字”:“Rachel”, “姓氏”:“豪厄尔”, “阿凡达”:https://reqres.in/img/faces/12-image.jpg" }, { “id”:13, “电子邮件”:“大师”。pythagoras@gmail.com", “名字”:“大师”, “姓氏”:“毕达哥拉斯”, “阿凡达”:https://reqres.in/img/faces/10-image.jpg" } ]

  • 帮助:我可以访问API并从本地API检索数据,但是当我按下按钮时,我不知道错误在哪里,因为当我更新按钮时,我的API根本没有更新,有人能帮我解决这个问题吗,提前谢谢

你的意思是它根本没有在点击更新按钮时调用
handleSubmit
?是的,先生,在const handleSubmit,我只是在我的API上添加手动数据进行更新,但我搞不清楚为什么不起作用。检查
按钮
组件中是否有
onSubmit
?它可能是
onClick