Reactjs 在本机react中循环获取的对象
我试图构建一个从远程URL获取JSON的应用程序,然后使用这个JSON构建几个视图 以下是我的应用程序代码:Reactjs 在本机react中循环获取的对象,reactjs,native,Reactjs,Native,我试图构建一个从远程URL获取JSON的应用程序,然后使用这个JSON构建几个视图 以下是我的应用程序代码: /** * DeepThoughts Template */ 'use strict'; var React = require('react-native'); var { AppRegistry, StyleSheet, Text, View, TouchableHighlight, } = React; var REQUEST_URL = 'http:/
/**
* DeepThoughts Template
*/
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight,
} = React;
var REQUEST_URL = 'http://www.geektime.co.il/appApi/main.json';
var AwesomeProject = React.createClass({
getInitialState: function() {
return {
thought: '',
};
},
componentDidMount: function() {
this.fetchData();
},
fetchData: function() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
thought: { title: responseData[0].title, content:responseData[0].content },
});
})
.done();
},
render: function() {
return (
<View style={styles.container}>
<View style={styles.textContainer}>
<Text style={styles.title}>
{this.state.thought.title}
</Text>
<Text style={styles.text}>
{this.state.thought.content}
</Text>
</View>
</View>
);
}
});
var Dimensions = require('Dimensions');
var windowSize = Dimensions.get('window');
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
},
textContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
title: {
fontSize: 30,
textAlign: 'center',
margin: 10,
},
text: {
fontSize: 18,
paddingLeft: 20,
paddingRight: 20,
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
buttonContainer: {
bottom: 0,
flex: .1,
width: windowSize.width,
backgroundColor: '#eee',
},
button: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
buttonText: {
fontSize: 30,
color: '#666666',
},
});
AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);
/**
*深度思维模板
*/
"严格使用",;
var React=require('React-native');
变量{
评估学,
样式表,
文本,
看法
触控高光,
}=反应;
var请求http://www.geektime.co.il/appApi/main.json';
var AwesomeProject=React.createClass({
getInitialState:函数(){
返回{
心想:",,
};
},
componentDidMount:function(){
这是fetchData();
},
fetchData:function(){
获取(请求URL)
.then((response)=>response.json())
.然后((响应数据)=>{
这是我的国家({
思想:{title:responseData[0]。title,content:responseData[0]。content},
});
})
.完成();
},
render:function(){
返回(
{this.state.think.title}
{this.state.think.content}
);
}
});
变量维度=要求(“维度”);
var windowSize=Dimensions.get('window');
var styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
对齐项目:“居中”,
背景颜色:“#FFFFFF”,
},
文本容器:{
弹性:1,
为内容辩护:“中心”,
对齐项目:“居中”,
},
标题:{
尺寸:30,
textAlign:'中心',
差额:10,
},
正文:{
尺码:18,
paddingLeft:20,
paddingRight:20,
textAlign:'中心',
颜色:'#333333',
marginBottom:5,
},
按钮容器:{
底部:0,
弹性体:.1,
宽度:windowSize.width,
背景颜色:“#eee”,
},
按钮:{
弹性:1,
对齐项目:“居中”,
为内容辩护:“中心”,
},
按钮文字:{
尺寸:30,
颜色:“#666666”,
},
});
AppRegistry.registerComponent('AwesomeProject',()=>AwesomeProject);
这是可行的,但是我还没有找到任何关于如何创建for或while循环的文档,甚至没有找到映射对象数组以便根据JSON中的项数创建视图的文档。这不完全是for循环,但本质上会调用表中每个元素的函数(在本例中,tables是我正在迭代的表数组),然后返回jsx div并将其添加到list中,因此当我将{list}放在render函数的return中时,它将显示所有元素,就像angularjs中的ng repeat一样。我希望这能有所帮助
render: function() {
console.log(this.props);
var i = 0;
var list = this.props.tables.map(function(tableDataProps){
if(i==4){
i=1;
return <div>
<div className='row' style={styles.border}></div>
<div className="col-sm-3 col-sm-12" style={styles.padding}>
<h1 style={styles.headings}>{tableDataProps.title}</h1>
<h2>{tableDataProps.id}</h2>
<CurrentTable jobs={tableDataProps} />
</div>
</div>
}
else{
i++;
return <div className="col-sm-3 col-sm-12" style={styles.padding}>
<h1 style={styles.headings}>{tableDataProps.title}</h1>
<CurrentTable jobs={tableDataProps} />
</div>
}
});
return <div>
{list}
</div>
}
render:function(){
console.log(this.props);
var i=0;
var list=this.props.tables.map(函数(tableDataProps){
如果(i==4){
i=1;
返回
{tableDataProps.title}
{tableDataProps.id}
}
否则{
i++;
返回
{tableDataProps.title}
}
});
返回
{list}
}