这是我当前的 app.js 文件。我希望按钮“知道”选择了哪个下拉菜单项,然后从列表中选择一首随机歌曲并将该歌曲名称写在屏幕上。
目前,无论您选择了哪种“心情”,它都只会在控制台中输出“快乐歌曲”的名称。
我相信我的错误出现在我的 If/If Else 语句中,但经过几个小时的调试/谷歌搜索,我找不到问题所在。
基本上,我需要一个函数来调用我的按钮的 onPress,在该函数中,我需要它来确定选择了哪个下拉菜单,并且只从歌曲的“心情”中输出一首随机的歌曲。但是,我当前的功能“macSong”将始终输出一首“快乐”的歌曲,即使下拉菜单选择了其他内容。
如果我的问题有任何令人困惑的地方,请在下面写下评论,让我知道我需要详细说明什么,谢谢!
import { StyleSheet, Text, TextInput, View, Button, Alert } from 'react-native';
import SearchableDropdown from 'react-native-searchable-dropdown';
var items =[
{
id: 1,
name: 'Happy Music'
},
{
id: 2,
name: 'Sad Music'
},
{
id: 3,
name: 'Chill Music'
},
{
id: 4,
name: 'Hype Music'
}
];
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
selectedItems: []
}
}
render() {
return (
<View style={ styles.screen }>
<Fragment>
{/* Title */}
<View style={ styles.title }>
<Text> Which Mac Miller Song Matches Your Mood? </Text>
</View>
{/* Single Dropdown Menu */}
<SearchableDropdown
onItemSelect={(item) => {
const items = this.state.selectedItems;
this.setState({ selectedItems: [...items, item]});
}}
containerStyle={{ padding: 25, alignSelf: 'center' }}
onRemoveItem={(item, index) => {
const items = this.state.selectedItems.filter((sitem) => sitem.id !== item.id);
this.setState({ selectedItems: items });
}}
itemStyle={{
padding: 10,
marginTop: 2,
backgroundColor: '#ddd',
borderColor: '#bbb',
borderWidth: 1,
borderRadius: 5,
}}
itemTextStyle={{ color: '#222' }}
itemsContainerStyle={{ maxHeight: 140 }}
items={items}
defaultIndex={2}
resetValue={false}
textInputProps={
{
placeholder: "What kind of music do you want to hear?",
underlineColorAndroid: "transparent",
style: {
padding: 12,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 5,
},
}
}
listProps={
{
nestedScrollEnabled: true,
}
}
/>
{/* Button */}
<View style={ styles.button }>
<Button
title="Press me for a Mac Miller song!"
onPress={() =>
this.macSong()}
/>
</View>
</Fragment>
</View>
);
}
/* Different Mood Function */
macSong(selectedItems) {
console.log(this.state.selectedItems)
if (this.state.selectedItems.includes('Happy Music')) {
let songs = ['best day ever', 'kool aid & frozen pizza', 'nikes on my feet']
let song = songs[Math.floor(Math.random() * songs.length)];
console.log(song);
} else if (this.state.selectedItems.includes('Sad Music')) {
let songs = ['self care', 'ROS', 'stay', 'whats the use']
let song = songs[Math.floor(Math.random() * songs.length)];
console.log(song);
} else if (this.state.selectedItems.includes('Chill Music')) {
let songs = ['good news', 'claymation', 'the star room']
let song = songs[Math.floor(Math.random() * songs.length)];
console.log(song);
} else if (this.state.selectedItems.includes('Hype Music')) {
let songs = ['donald trump', 'remember', 'weekend']
let song = songs[Math.floor(Math.random() * songs.length)];
console.log(song);
} else {
console.log("Selected Item is Unknown")
}
}
}
{/* StyleSheet */}
const styles = StyleSheet.create({
screen: {
backgroundColor: ''
},
button: {
padding: 10,
alignSelf: 'center'
},
title: {
padding: 30,
alignSelf: 'center',
textAlign: 'center'
}
});