I'd like to create an external function that fetch data and return the result.
But I can't make it work, here's my function:
// Request.js:
import React from 'react'
import { authHeader } from "../../jwt/_helpers/auth-header";
import { handleResponse } from "../../jwt/_helpers/handle-response";
export default function Request(method,request) {
const requestOptions = {
method: method,
withCrendtials: true,
headers: authHeader(),
};
fetch(process.env.REACT_APP_API + request, requestOptions)
.then(handleResponse)
.then(
(result) => {
console.log(result); // This returns an object
return result // This returns undefined
},
(error) => {
return error
}
);
}
Then I use it like so:
// Main.js:
import Request from './Request';
let obj = Requete("GET", "/myPath/myQuery");
this.setState({foo: obj.items});
In my Request.js file, i'm logging the result and I get the right object, then I return it and In my Main.js I get an undefined value for that function.
If I copy my code from Request.js to my Main.js it works fine...
But my code gets unreadable with fetch queries taking almost 20 lines each...
Any ideas?