如何使用条件语句创建函数以确定按下按钮时的功能? (本机反应)

How to make a Function with Conditional Statements to determine what a button does when pressed? (React Native)

这是我当前的 app.js 文件。我想要 "know" 选择哪个下拉菜单项的按钮,然后从列表中随机选择一首歌曲并将该歌曲名称写在屏幕上。

目前,无论您选择了哪个"mood",控制台只会输出"happy song"个名字。

我相信我的错误是在我的 If/If Else 语句中的某个地方,但是在 debugging/googling 几个小时之后,我找不到问题所在。

基本上,我需要一个函数来调用按钮的 onPress,在那个函数中,我需要它来确定选择了哪个下拉菜单,并且只从 "mood" 的 "mood" 中输出一首随机歌曲歌曲。但是,我当前的功能 "macSong" 将始终输出 "happy" 歌曲,即使下拉菜单选择了其他内容也是如此。

如果我的问题有任何令人困惑的地方,请在下面发表评论,让我知道我需要详细说明的内容,谢谢!

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'
  }
});

在您的 onItemSelect={(item) => {} 方法中,您通过将对 this.state.selectedItems 的引用引用到 "items" 常量并向其推送一个新值来改变您的状态,您应该它像这样不可变:

onItemSelect={(item) => {
  const items = this.state.selectedItems;
  this.setState({ selectedItems: [...items, item]});
}}

然后在 Difference moods 函数中使用单个“=”,这意味着分配,而不是比较。只需使用

if (selectedItems === 'Happy Music'){

在你的 if 语句中,我想它会像你期望的那样工作