I'm learning React and able to pull API data inside of my various components.
I make Axios calls like this:
componentDidMount(){
axios.get("https://api.someurl.com/some-request/").then((response)=>{
this.setState(()=>{
return {
mydata: response.data
}
})
})
}
This all works so my next step is to move the API calls to an external function/file and pass parameters to make the API calls reusable. This is mainly because the API needs to be secured with authorization headers, time stamps, etc. so I'd like it all done in one place. Although I can't seem to find the right approach.
What I'm trying to do:
// Inside of components:
componentDidMount(){
this.setState(()=>{
return {
mydata: externalFunction({this.state.someArg})
}
})
}
// Some external module
function externalFunction(args) {
// create authorization header, make call
return { "name":"John", "age":25, "city":null }
}
Should the external module be React class (I don't need state - it's just JSON data), a react function or can I simply use a JavaScript function? No matter what I try it seems the return values of the Axios calls are undefined. Again, I'm new to this so probably missing something critical...
Any example would be appreciated. Thanks.