我有一个存储菜单的JSON对象。默认情况下,第一个菜单将显示。在任何菜单项下,我想显示针对" SEQ"定义的菜单编号。这是我的json文件**菜单。
{"main":
[
{"mid": 0, "menu":
[
{"id": "1", "title": "Item-1-0", "kw": "Item-1", "type" : "M", "seq" : 1},
{"id": "2", "title": "Item-2", "kw": "Item-2", "type" : "M", "seq" : 2},
{"id": "3", "title": "Item-3", "kw": "Item-3", "type" : "M", "seq" : 3},
{"id": "4", "title": "Item-4", "kw": "Item-4", "type" : "M", "seq" : 4},
{"id": "5", "title": "Item-5", "kw": "Item-5", "type" : "M", "seq" : 5},
{"id": "6", "title": "Item-6", "kw": "Item-6", "type" : "M", "seq" : 6},
{"id": "7", "title": "Item-7", "kw": "Item-7", "type" : "M", "seq" : 7},
{"id": "8", "title": "Item-8", "kw": "Item-8", "type" : "M", "seq" : 8}
]
},
{"mid": 1, "menu":
[
{"id": "1", "title": "Item-1-1", "kw": "Item-1", "type" : "M", "seq" : 8},
{"id": "2", "title": "Item-2", "kw": "Item-2", "type" : "M", "seq" : 9},
{"id": "3", "title": "Item-3", "kw": "Item-3", "type" : "M", "seq" : 10},
{"id": "4", "title": "Item-4", "kw": "Item-4", "type" : "M", "seq" : 11},
{"id": "5", "title": "Item-5", "kw": "Item-5", "type" : "M", "seq" : 12},
{"id": "6", "title": "Item-6", "kw": "Item-6", "type" : "M", "seq" : 13},
{"id": "7", "title": "Item-7", "kw": "Item-7", "type" : "M", "seq" : 14},
{"id": "8", "title": "Item-8", "kw": "Item-8", "type" : "M", "seq" : 15}
]
},
{"mid": 3, "menu":
[
{"id": "1", "title": "Item-1-2", "kw": "Item-1", "type" : "M", "seq" : 16},
{"id": "2", "title": "Item-2", "kw": "Item-2", "type" : "M", "seq" : 17},
{"id": "3", "title": "Item-3", "kw": "Item-3", "type" : "M", "seq" : 18},
{"id": "4", "title": "Item-4", "kw": "Item-4", "type" : "M", "seq" : 19},
{"id": "5", "title": "Item-5", "kw": "Item-5", "type" : "M", "seq" : 20},
{"id": "6", "title": "Item-6", "kw": "Item-6", "type" : "M", "seq" : 21},
{"id": "7", "title": "Item-7", "kw": "Item-7", "type" : "M", "seq" : 22},
{"id": "8", "title": "Item-8", "kw": "Item-8", "type" : "M", "seq" : 23}
]
},
{"mid": 4, "menu":
[
{"id": "1", "title": "Item-1-3", "kw": "Item-1", "type" : "M", "seq" : 24},
{"id": "2", "title": "Item-2", "kw": "Item-2", "type" : "M", "seq" : 25},
{"id": "3", "title": "Item-3", "kw": "Item-3", "type" : "M", "seq" : 26},
{"id": "4", "title": "Item-4", "kw": "Item-4", "type" : "M", "seq" : 27},
{"id": "5", "title": "Item-5", "kw": "Item-5", "type" : "M", "seq" : 28},
{"id": "6", "title": "Item-6", "kw": "Item-6", "type" : "M", "seq" : 29},
{"id": "7", "title": "Item-7", "kw": "Item-7", "type" : "M", "seq" : 30},
{"id": "8", "title": "Item-8", "kw": "Item-8", "type" : "M", "seq" : 31}
]
}
]
}
我的 app.js 文件是:
import React from 'react';
import { Button, View, Text } from 'react-native';
import GetMenu from "./fetchmenu.js";
class HomeScreen extends React.Component {
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
{<GetMenu/>}
</View>
</View>
);
}
}
export default HomeScreen;
和getMenu类在此文件中
import React from 'react';
import { FlatList, ActivityIndicator, Text, View, TouchableOpacity } from 'react-native';
let dataSource = require('./menu.json');
let menuLevel = 0;
export default class GetMenu extends React.Component {
constructor(props){
super(props);
}
render(){
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={dataSource.main[menuLevel].menu}
renderItem={({item}) =>
(
<TouchableOpacity onPress={ () => actionOnRow(item)}>
<Text>{item.title}, {item.kw}, {item.seq}</Text>
</TouchableOpacity>
)}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
function actionOnRow(item) {
menuLevel = item.seq;
alert(item.title + "-" + menuLevel);
render() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
<View style={{ flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'center' }}>
{<GetMenu/>}
</View>
</View>
);
}
}
它在 actionOnrow 函数
上崩溃()在Render()上崩溃我应该这样做不同,而不是递归调用 actionOnrow onpress?
编辑
一旦我经过此阶段,我想根据"类型"在ActionOnrow()中添加条件。" M"值将调用下一个菜单,而其他一些值(例如" L")将调用外部JSON显示音频列表。
让我首先告诉你你的错误是什么。按下菜单时,您正在尝试渲染组件。但是您在菜单下的事件中调用渲染函数,但是您无法在onPress
事件中进行渲染功能。您可以在这里做的是设置一些标志,指示是否呈现内部菜单。在代码中,就是这样:
//render item of your flatList
<TouchableOpacity onPress={ () => this.setState({renderMenu:true,menuItem:item})}>
<Text>{item.title}, {item.kw}, {item.seq}</Text>
</TouchableOpacity>
//place where you want to render your menu
{this.state.renderMenu&&actionOnRow(this.state.menuItem)}
替代
您可以将当前菜单存储到状态,并在按下菜单时进行更新。示例代码:
state={currentMenu:dataSource.main[menuLevel].menu}
<FlatList
data={this.state.currentMenu}
renderItem={({item}) =>
(
<TouchableOpacity onPress={ () => this.setState({currentMenu:item})}>
<Text>{item.title}, {item.kw}, {item.seq}</Text>
</TouchableOpacity>
)}
keyExtractor={({id}, index) => id}
/>
注意:我没有测试所有代码。他们只是例子。让我知道您是否不了解任何东西。