首先,我想先說我是React Native和Shoutem的總noob。我不確定是否應該在我的問題中寫下Shoutem或React Native,但這是我的問題。Shoutem提取數據不顯示
我有兩個屏幕:
- Screen1.js
- Screen2.js
屏蔽1顯示從獲取返回的項目列表。一旦我點擊該項目,它將打開第二個屏幕,這是詳細信息屏幕。
我正在將數據從screen1傳遞給screen2。在screen2中,我需要爲不同的數據進行另一次獲取調用,但它不起作用。我在兩個屏幕上都做了完全相同的事情。
這裏是我的屏蔽1代碼:
import React, {
Component
} from 'react';
import {
ActivityIndicator,
TouchableOpacity
} from 'react-native';
import {
View,
ListView,
Text,
Image,
Tile,
Title,
Subtitle,
Overlay,
Screen
} from '@shoutem/ui';
import {
NavigationBar
} from '@shoutem/ui/navigation';
import {
navigateTo
} from '@shoutem/core/navigation';
import {
ext
} from '../extension';
import {
connect
} from 'react-redux';
export class List extends Component {
constructor(props) {
super(props);
this.renderRow = this.renderRow.bind(this);
this.state = {
isLoading: true,
content: null,
}
}
componentDidMount() {
return fetch('https://www.cannabisreports.com/api/v1.0/strains').then((response) => response.json()).then((responseData) => {
this.setState({
isLoading: false,
content: responseData
});
}).done();
}
renderRow(rowData) {
const { navigateTo } = this.props;
return (
//<Text>{rowData.name}, {rowData.createdAt.datetime}</Text>
<TouchableOpacity onPress={() => navigateTo({
screen: ext('Strain'),
props: { rowData }
})}>
<Image styleName="large-banner" source={{ uri: rowData.image &&
rowData.image ? rowData.image : undefined }}>
<Tile>
<Title>{rowData.name}</Title>
<Subtitle>none</Subtitle>
</Tile>
</Image>
</TouchableOpacity>
);
}
render() {
if (this.state.isLoading) {
return (
<View style={{flex: 1, paddingTop: 20}}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{flex: 1, paddingTop: 20}}>
<ListView
data={this.state.content.data}
renderRow={rowData => this.renderRow(rowData)}
/>
</View>
);
}
}
// connect screen to redux store
export default connect(
undefined,
{ navigateTo }
)(List);
我傳遞rowData
到畫面2。然後我需要使用來自rowData
的數據進行另一次獲取調用,因爲它是Screen2中API調用所需的路徑參數。
所以基本上我需要在屏幕2一fetch調用是這樣的:
fetch('https://mywebsite.com/'+rowData.something+'/myotherdata')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
content: responseJson.data
})
})
.catch((error) => {
console.error(error);
});
這裏是我的屏幕2代碼:
export default class Strain extends Component {
constructor(props) {
super(props);
this.state = {
content: null,
}
}
componentDidMount() {
return fetch('https://mywebsite.com/'+rowData.something+'/myotherdata')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
content: responseJson.data
})
})
.catch((error) => {
console.error(error);
});
}
renderRow(dataContent) {
return (
<Text>{dataContent.name}</Text>
// This does not work either -> <Text>{dataContent}</Text>
);
}
render() {
const { rowData } = this.props; //This is coming from screen1.js
return (
<ScrollView style = {{marginTop:-70}}>
<Image styleName="large-portrait" source={{ uri: rowData.image &&
rowData.image ? rowData.image : undefined }}>
<Tile>
<Title>{rowData.name}</Title>
<Subtitle>{rowData.createdAt.datetime}</Subtitle>
</Tile>
</Image>
<Row>
<Text>Seed Company: {rowData.seedCompany.name}</Text>
</Row>
<Divider styleName="line" />
<Row>
<Icon name="laptop" />
<View styleName="vertical">
<Subtitle>Visit webpage</Subtitle>
<Text>{rowData.url}</Text>
</View>
<Icon name="right-arrow" />
</Row>
<Divider styleName="line" />
<View style={{flex: 1, paddingTop: 20}}>
<ListView
data={content}
renderRow={dataContent => this.renderRow(dataContent)}
/>
</View>
<Divider styleName="line" />
</ScrollView>
);
}
}
我取URL返回的數據是這樣的:
{
data: {
name: "7.5833",
another_name: "8.6000",
different_name: "5.7500",
}
}
這隻會返回一個數據對象,就像您在上面看到的一樣。
當我運行代碼,我得到這個錯誤: null is not an object (evaluating 'Object.keys(e[t])')
請讓我知道如果你需要我提供更多的信息。
我已經嘗試了很多不同的東西,似乎沒有任何工作,所以我需要一些幫助。我究竟做錯了什麼?
你知道哪些代碼調用Object.keys(e [t])'?如果沒有,你需要去瀏覽你的瀏覽器devtools並找出答案。此外,還不清楚問題中的render()代碼如何與您的獲取調用相關。實際調用'render()'的代碼在哪裏?在'this.setState({content_responseJson。data },function(){ //用新狀態做某事 });'那是什麼代碼'//對新狀態部分實際調用?這與問題中的其他代碼有什麼關係? – sideshowbarker
@iamthestreets你在哪裏看到這個錯誤?看起來像是縮小代碼中的錯誤,所以很難說,嘗試運行調試版本以獲得更友好的錯誤。從我可以告訴你沒有給你的'ListView'正確的'內容'。通過'this.state.content'或做'const {content} = this.state;'作爲渲染函數的第一行。最好的做法是先解構(第二種方法),如果「內容」不可用,則呈現佔位符。 – dodsky
我使用Shoutem Builder來運行應用程序,因爲這是我得到任何錯誤顯示的唯一方法。 @sideshowbarker我刪除了'function(){//用新狀態做某事}'不需要它。我不確定你的代碼調用'render()是什麼意思。當屏幕加載它時調用'render()'。 – iamthestreets