我在另一个文件中有一个对象数组,我将其映射到2个文件中怎么办:在react-native中使用e.target.src,因为在react-native中我们没有e
//存储文件:
export default [
{
liked: false,
},
{
liked: false,
},
{
liked: false,
},
{
liked: false,
},
]
// 2文件
import images from 'store.js'
function unlike (){
// in react will be """ e.target.src = '../assets/unliked.png' """ but how in react-native ?
}
function like (){
// in react will be """ e.target.src = '../assets/liked.png' """ but how in react-native ?
}
return(
<ScrollView>
{images.map(image => {
return({image.liked ? <Image onPress={unlike} source={require('../assets/liked.png')} /> : <Image onPress={unlike} source={require('../assets/unliked.png')} />
</ScrollView >
)
发布于 2021-10-31 01:34:48
您可以预先导入两张图片,也可以动态设置是否基于liked
的图片来源。要在每个喜欢或不喜欢的地方重新呈现UI,请使用状态:
import images from 'store.js'
import React, { useState, useEffect } from 'react';
// other imports
const liked = require('../assets/liked.png')
const disliked = require('../assets/unliked.png')
const LikesComponent = (props) => {
const [likesState, setLikesState] = useState(images)
const handleLikes = (index) => {
let temp = [...likesState]
temp[index].liked = !temp[index].liked
setLikesState(temp)
}
return(
<ScrollView>
{
likesState.map((image, index) => <Image key={index}
source={image.liked ? liked : disliked}
onPress={() => handleLikes(index)} />
}
</ScrollView>
)
}
export default LikesComponent
https://stackoverflow.com/questions/69784983
复制