Reactjs 如何在React Native中列出传入数据
我想列出我使用的服务中的数据,如示例中所示。你能帮我吗 我的代码:Reactjs 如何在React Native中列出传入数据,reactjs,react-native,Reactjs,React Native,我想列出我使用的服务中的数据,如示例中所示。你能帮我吗 我的代码: import React, { Component } from "react"; export default class CustomersTab extends Component { constructor(props) { super(props); this.state = { token: "", isLoading: true, dataSource:
import React, { Component } from "react";
export default class CustomersTab extends Component {
constructor(props) {
super(props);
this.state = {
token: "",
isLoading: true,
dataSource: null
};
}
componentWillMount() {
tokenner()
.then(responseJson => {
const token = "Bearer " + responseJson.result.token;
this.setState({ token });
fetch(
"apiurl",
{
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: token
}
}
)
.then(response => response.json())
.then(responseData => {
this.setState({
isLoading: false,
dataSource: responseData.result
});
});
})
.catch(error => console.warn(error));
}};
render() {
return (
<View>
<Text>
I WANT LİST DATA HERE
</Text>
</View> )}}
import React,{Component}来自“React”;
导出默认类CustomersTab扩展组件{
建造师(道具){
超级(道具);
此.state={
令牌:“”,
孤岛加载:是的,
数据源:空
};
}
组件willmount(){
tokenner()
.然后(responseJson=>{
const token=“Bearer”+responseJson.result.token;
this.setState({token});
取回(
“apiurl”,
{
方法:“获取”,
标题:{
接受:“应用程序/json”,
“内容类型”:“应用程序/json”,
授权:令牌
}
}
)
.then(response=>response.json())
.然后(响应数据=>{
这是我的国家({
孤岛加载:false,
数据源:responseData.result
});
});
})
.catch(错误=>console.warn(错误));
}};
render(){
返回(
我想要这里的最新数据
)}}
我试着自己做,但我无法构建循环结构。每次仅显示一个数据。我该怎么办?你必须这样做。将id更改为要显示的内容
return (
<View>
<ListView
dataSource={this.state.dataSource}
renderRow={data => <Text>{data.id}</Text>}
/>
</View>
);
返回(
{data.id}
/>
);
请从react native导入视图和文本
import {
Text,
View,
} from 'react-native';
constructor(props: Object) {
super(props);
this.state = {
dataSource: []
}
};
/*数据源=[
{“id”:“1”,“标题”:“星球大战”,“发行年份”:“1977”},
{“id”:“2”,“标题”:“回到未来”,“发布年份”:“1985”},
{“id”:“3”,“标题”:“矩阵”,“发布年份”:“1999”},
{“id”:“4”,“标题”:“起始”,“发布年份”:“2010”},
{“id”:“5”,“标题”:“星际”,“释放年”:“2014”}
]
*/
renderMovieList(){
返回this.state.dataSource.map((电影,索引)=>
电影名称
电影年
)
}
render(){
返回(
{this.state.dataSource.length?this.renderMovieList():null}
);
}
将从服务器接收到的数据保存在一个数组中,然后在Flatlist数据中使用。如何使用?您可以进行en expamle吗?在这里,您可以保存接收到的数据并保存在类似“data”的数组中:if(response.data.status==“success”){console.log(response);if(response.data.data.length>0){this.setState({data:response.data.data});}else{alert(“”;}然后在平面列表中这样使用它:如何使它更直观?{this.state.dataSource.length?this.renderMovieList():null}不工作?使用空数组初始化数据源。我已更新了答案。请检查itresponseData。结果是数组或对象。