很抱歉,如果有人问我,我是本机反应中的新手...
我有一个简单的应用程序,它呈现一些类别,并且数据位于具有数组的对象中的一个js文件中:
tsc
我想使用同一文件只是为了连接到API并获取数据。
我尝试了很多类似的事情:
export default tabs = {
categories: [
{ id: 'sample', title: 'sample' },
{ id: 'sample2', title: 'sample2' },
],
deals: [
{ id: 'sample', title: 'sample' },
{ id: 'sample2', title: 'sample2' },
],
};
它永远不会在日志中得到任何东西...尝试了许多其他方法。.
任何人都有类似的东西
提前感谢!
到目前为止,我没有出现任何错误:
import React from 'react';
const APIcategories = '......';
export default {
componentDidMount() {
console.log('inside... ');
fetch(APIcategories)
.then((res) => res.json())
.then((json) => this.setState({ data: json }));
},
};
答案 0 :(得分:0)
创建一个类,然后将其实际用于其方法。
class SomeClass extends Component {
constructor(props) {
super(props);
this.state = {
loading: false,
data: [],
error: null,
};
this.arrayholder = [];
}
componentDidMount() {
this.someGetRequest();
}
someGetRequest = () => {
const url = `https:...`;
this.setState({ loading: true });
fetch(url)
.then(res => res.json())
.then(res => {
this.setState({
data: res.results,
error: res.error || null,
loading: false,
});
this.arrayholder = res.results;
console.log(res.results)
})
.catch(error => {
this.setState({ error, loading: false });
});
};
export default SomeClass;
此代码已经通过expo-cli 3.18.4进行了测试