[英]How to keep Reactjs API calling function in independent page and call from component?
This code is working fine now I want to move api calling function in to separate page这段代码现在工作正常我想将调用 function 的 api 移动到单独的页面
const Login = () => {
const handleSubmit = (event) => {
axios.post('/api/login', { email: email, password: password})
.then((result) => {
let resultEmail = result.email;
console.log( resultEmail )
dispatch({ type: 'LOGIN', login: { email } });
console.log(result)
});
}
}
=========================== ============================
Please check my code that I added below.. Problems请检查我在下面添加的代码..问题
How can I call login function from login component?如何从登录组件调用登录 function?
CONSOLE ERROR - TypeError: _services__WEBPACK_IMPORTED_MODULE_3__.UseApi.login is not a function控制台错误 - 类型错误:_services__WEBPACK_IMPORTED_MODULE_3__.UseApi.login 不是 function
//API services page //API服务页面
export const UseApi = () => {
const { dispatch } = useContext(GlobalContext);
const login = (email, password) => {
axios.post('/api/login', { email: email, password: password})
.then((result) => {
let resultEmail = result.email;
console.log( resultEmail )
dispatch({ type: 'LOGIN', login: { email } });
console.log(result)
});
}
}
//login component
import UseApi from "./services" ;
function Login() {
const handleSubmit = (event) => {
UseApi.login(email, password)
}
};
edited: services.js编辑:services.js
import axios from "axios";
import React, { useState, useContext, useEffect } from "react";
import { GlobalContext } from "../../store/globalProvider";
const { dispatch } = useContext(GlobalContext);
const Login = (email, password) => {
axios
.post("/api/login", { email: email, password: password })
.then(result => {
let resultEmail = result.email;
console.log(resultEmail);
dispatch({ type: "LOGIN", login: { email } }); //dispatch function will pass the action up to BookContext
console.log(result);
});
};
useEffect(() => {});
export const UseApi = {
Login
};
let me know if this works for you or not让我知道这是否适合您
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.