Json 平面列表的数据中是否可能有多个参数?
例如:Json 平面列表的数据中是否可能有多个参数?,json,reactjs,react-native,jsx,Json,Reactjs,React Native,Jsx,例如: <FlatList data={ this.state.foods, this.state.trees } renderItem={({item}) => <View><Text>{item.food}</Text><Text>{item.tree}</Text></View> /> 本州树木: 编辑: render(){ const {trees, foo
<FlatList data={ this.state.foods, this.state.trees }
renderItem={({item}) => <View><Text>{item.food}</Text><Text>{item.tree}</Text></View>
/>
本州树木:
编辑:
render(){
const {trees, foods} = this.state
const combinedArray = trees.map(tree => Object.assign(tree, foods.find(food => food.id == tree.id)));
return(
componentDidUpdate(prevProps, prevState) {
if (!prevState.foods) {
fetch('https://www.example.com/React/food.php')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
foods: responseJson.map(item => ({ ...item, progress: new Animated.Value(0), unprogress: new Animated.Value(1) }))
}, function() {
});
})
.catch((error) => {
//console.error(error);
});
fetch('https://www.example.com/React/tree.php')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
trees: responseJson
}, function() {
});
})
.catch((error) => {
//console.error(error);
});
}
}
您需要向平面列表提供单个数组 您可以根据id对对象进行分组并使用它们
const {trees, foods} = this.state
const combinedArray = trees.map(tree => Object.assign(tree, foods.find(food => food.id == tree.id)));
然后在平面列表中使用combinedArray
如果您的状态最初未定义或为空,则需要添加
constructor(props) {
super(props)
this.state = {
trees: [],
foods: []
}
}
到您的类组件。好的,我在代码中完成了这一实现,但我得到了一个错误:undefined不是一个计算“trees.map”的对象。您可以发布使用该对象的完整函数,以及如何初始化状态。同时添加初始状态定义。对不起,这是什么意思?你是说我从哪里取的并把它改成这个.state?初始状态,在这里你初始化了你的状态,要么在构造函数中,要么直接作为一个类变量
const {trees, foods} = this.state
const combinedArray = trees.map(tree => Object.assign(tree, foods.find(food => food.id == tree.id)));
constructor(props) {
super(props)
this.state = {
trees: [],
foods: []
}
}