So I`m trying to create a service to be used in the entire project which makes an API call to retrieve some data from the server. The function looks something like this
export function data(){
const proxyurl = "https://cors-anywhere.herokuapp.com/";
fetch(proxyurl+process.env.REACT_APP_TEXT_API)
.then(res => res.json())
.then(json => {
return json;
})
}
I`m trying to access the returned json in my React component by doing this
import {data} from '../../../../services/textService/textApi'
class Error404Page extends React.Component {
constructor(props) {
super(props);
this.state = {
data: "Something"
}
}
componentDidMount() {
this.setState({
data: this.data()
})
}
The problem is the state variable "data" value is set to undefined. How can I access the data returned by my function?