获取在 const 中设置的产品 ID 在反应本机循环中使用 onPress
get product id set in const use onPress in react native loop
我想在pid中添加key={item.id}值使用onPress,而她的数据来到数据库在react native中使用循环
const addToWishlist = () => {
const [pid, setPid] = useState('');
}
return (
<>
{ProductData.map((item, index) => {
return (
<View key={index} style={styles.prod}>
<TouchableOpacity onPress={addToWishlist} key={item.id}>
<Feather name="heart" color={heartColor} style={{fontSize:16}}/>
</TouchableOpacity>
</View>
)
})}
</>
)
您的函数应该接受一个参数 id
并且您必须在触发 onPress
时发送它。
onPress={() => addToWishlist(item.id)}
const addToWishlist = (id) => {
console.log(id)
// const [pid, setPid] = useState('');
// You should not declare a hook in a function
}
return (
<>
{ProductData.map((item, index) => (
<View key={index} style={styles.prod}>
<TouchableOpacity onPress={() => addToWishlist(item.id)} key={item.id}>
<Feather name="heart" color={heartColor} style={{fontSize:16}}/>
</TouchableOpacity>
</View>
)})
</>
)
我想在pid中添加key={item.id}值使用onPress,而她的数据来到数据库在react native中使用循环
const addToWishlist = () => {
const [pid, setPid] = useState('');
}
return (
<>
{ProductData.map((item, index) => {
return (
<View key={index} style={styles.prod}>
<TouchableOpacity onPress={addToWishlist} key={item.id}>
<Feather name="heart" color={heartColor} style={{fontSize:16}}/>
</TouchableOpacity>
</View>
)
})}
</>
)
您的函数应该接受一个参数 id
并且您必须在触发 onPress
时发送它。
onPress={() => addToWishlist(item.id)}
const addToWishlist = (id) => {
console.log(id)
// const [pid, setPid] = useState('');
// You should not declare a hook in a function
}
return (
<>
{ProductData.map((item, index) => (
<View key={index} style={styles.prod}>
<TouchableOpacity onPress={() => addToWishlist(item.id)} key={item.id}>
<Feather name="heart" color={heartColor} style={{fontSize:16}}/>
</TouchableOpacity>
</View>
)})
</>
)