我正在开发我的第一个React原生应用程序,需要使用fontAwesome作为明星图标.我需要使用onPress函数来更新highlight变量并更改恒星的 colored颜色 .但当我试着在expo 上运行它并按下一颗星星时,什么都没发生,变量保持不变.

import { StatusBar } from 'expo-status-bar';
import { StyleSheet, Text, View, FlatList, Image, Button } from 'react-native';
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome';
import { faStar } from '@fortawesome/free-solid-svg-icons'
import React, {useState, useEffect} from 'react'

export default function Detail(props) {
  const movie = props.navigation.getParam('movie', null) 
  const [ highlight, setHeighlight] = useState(0)

  const rateclicked = () => {
      console.log(highlight);
  }


  return (
    <View style={styles.container}>

        <View style={styles.starContainer}>
            <FontAwesomeIcon style={movie.avg_rating > 0 ? styles.orange : styles.white} icon={faStar}/>
            <FontAwesomeIcon style={movie.avg_rating > 1 ? styles.orange : styles.white} icon={faStar}/>
            <FontAwesomeIcon style={movie.avg_rating > 2 ? styles.orange : styles.white} icon={faStar}/>
            <FontAwesomeIcon style={movie.avg_rating > 3 ? styles.orange : styles.white} icon={faStar}/>
            <FontAwesomeIcon style={movie.avg_rating > 4 ? styles.orange : styles.white} icon={faStar}/>
            <Text style={styles.white}>({movie.no_of_ratings})</Text>
        </View>

        <Text style={styles.description}> {movie.description}</Text>
        <View style={{borderBottomColor: 'white', borderBottomWidth: 2}}/>
        <Text style={styles.description}>Rate it !!!</Text>

        <View style={styles.starContainer}>
            <FontAwesomeIcon style={highlight > 0 ? styles.purple : styles.grey} icon={faStar} size={48} onPress={()=> setHeighlight(1)}/>
            <FontAwesomeIcon style={highlight > 1 ? styles.purple : styles.grey} icon={faStar} size={48} onPress={()=> setHeighlight(2)}/>
            <FontAwesomeIcon style={highlight > 2 ? styles.purple : styles.grey} icon={faStar} size={48} onPress={()=> setHeighlight(3)}/>
            <FontAwesomeIcon style={highlight > 3 ? styles.purple : styles.grey} icon={faStar} size={48} onPress={()=> setHeighlight(4)}/>
            <FontAwesomeIcon style={highlight > 4 ? styles.purple : styles.grey} icon={faStar} size={48} onPress={()=> setHeighlight(5)}/>
            
        </View>
        <Button title="Rate" onPress={() => rateclicked()}/>
    </View>
  );
}

Detail.navigationOptions = screenProps => ({
    title: screenProps.navigation.getParam('title'),
    headerStyle: {
        backgroundColor: 'orange',

    },
    headerTintColor: '#fff',
    headerTitleStyle: {
        fontWeight: 'bold',
        fontSize: 24,
    },
    headerRight: () => {
        <Button title="Edit" color="white"
        onPress={() => screenProps.navigation.navigate("Edit", {movie: screenProps.navigation.getParam("movie")})} />
    
    },


})

const styles = StyleSheet.create({
  container: {
    flex: 1, 
    padding: 10,
    backgroundColor: '#282C35',
    
  },

  item: {
    flex: 1,
    padding: 10,
    height: 50,
    backgroundColor: '#282C35'

  },

  itemText: {
    color:'#ffff',
    fontSize: 24,
  },

  starContainer: {
    alignItems: "center",
    justifyContent: "center",
    flexDirection: "row",
  },

  orange: {
      color: 'orange',

  },

  white: {
      color: 'white',
  },

  description: {
    fontSize: 20,
    color: 'white',
    padding: 10,
  },

  purple: {
    color: 'purple'
  },

  grey: {
      color: 'grey'
  },
});

我如何解决这个问题?

推荐答案

在React Native中,并非所有内容都可以单击,因为它与HTML元素一样.FontAwesomeIcon是不具有onPress属性的组件之一.例如,您需要将其包装在一个Pressable组件中,如下所示:

// import it at the top of the component
import { Pressable } from "react-native"
<Pressable onPress={()=> setHeighlight(1)}>
  <FontAwesomeIcon style={highlight > 0 ? styles.purple : styles.grey} icon={faStar} size={48} />
</Pressable>

Javascript相关问答推荐

如何获取转换字节的所有8位?

如何分配类型脚本中具有不同/额外参数的函数类型

每次子路由重定向都会调用父加载器函数

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

如何在Javascript中使用Go和检索本地托管Apache Arrow Flight服务器?

如何让npx在windows中运行js脚本?

在执行异步导入之前判断模块是否已导入()

在我的index.html页面上找不到我的Java脚本条形图

当我try 将值更改为True时,按钮不会锁定

将范围 Select 器添加到HighChart面积图

如何在下一个js中更改每个标记APEXCHARTS图表的 colored颜色

通过解构/功能组件接收props-prop验证中缺少错误"

对不同目录中的Angular material 表列进行排序

如何设置时间选取器的起始值,仅当它获得焦点时?

在范围数组中查找公共(包含)范围

如何创建一个for循环,用于计算仪器刻度长度并将其放入一个HTML表中?

验证Java脚本函数中的两个变量

在Reaction Native中,ScrolltoIndex在结束时不一致地返回到索引0

从客户端更新MongoDB数据库

TS node 找不到依赖的模块