Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

How to Load Images Dynamicaly for React Native Navigation Carousel?

I am currently developing a sheet music app that uses a carousel. I push the song_item data from the previous screen to the new screen. The song_path in the loop exists in the console.log. What i need is for images2 to be the same as the images array. The images array is hardcoded for development purpuses. Thank you.

const { itemId, songs_item } = route.params;
console.log("SONGS #################",songs_item['songs'])

var songs = songs_item['songs']
var images = [
    require('../assets/songs/lala.jpg'),
    require('../assets/songs/δοκιμη.jpg'),
    require('../assets/songs/lala.jpg'),
  ]

let images2 = []
songs.forEach((song, i) => {
    const song_path = "../assets/"+song['path'];
    // const rew = require(song_path);
    console.log(song_path)
    // images2.push(rew);    
})

It throws an invalid call for require when uncommenting the lines.

UPDATE

Also why cant it work when i uncomment const path = song['path'] but it works when const path = 'songs/lala.jpg';?

let images2 = [];
songs_item['songs'].forEach((song, i) => {
console.log(song['path'])
const path =  'songs/lala.jpg';
//const path = song['path']
// console.log('PATHHHHHHHHHHHH',path)
images2.push(require('../assets/'+path))

})


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...