I have a react-native-modal-dropdown with multiple options. When the user selects an option, changeImage() is triggered and the image path updates from over 150 possible images.
However the following solution doens't update the Image component because react native doesn't allow dynamic require.
const [imagePath,setImagePath] = useState('defaultImg')
// newImg, is a dynamic string which changes with user input
// "assets/images" folder has over 150 images
const changeImage = (newImg) {
// if valid path, change path
setImagePath(newImage)
}
return(
<Image
style={styles.imageStyle}
resizeMode="center"
source={require('../assets/images/' + imagePath + '.png')}
/>)
This solution is not scalable, neither is this.
I've seen some solutions using json and helper methods but I couldn't get them to work.