如何通过本机上下文传递数组



嗨,亲爱的,感谢您回答我的问题,我正在尝试使用react本机上下文将数组从一个组件传递到另一个组件。下面是我的尝试,但它不适用于数组

import React from 'react';

var context = {
playlist: null, //like I want to make this value a array
setPlaylist: (songID: string) => {
}
}
export var playlistContext = React.createContext(context);

我的App.tsx文件

const [playlist, setPlaylist] = useState<string | null>(null);
<playlistContext.Provider value={{
playlist,
setPlaylist: (songID: string) => setPlaylist(songID),
}}>
<Navigation colorScheme={colorScheme} />
<StatusBar />
<PlayerWidget />
</playlistContext.Provider>

以下是通过如下方式改革playlistContext.tsx上下文API得到的答案:

import React from 'react';

var context = {
playlist: [],
setPlaylist: (songID: string[]) => {
}
}
export var playlistContext = React.createContext(context);

然后我在我的App.tsx中做了这件事,所以必须在提供商中使用它

const [playlist, setPlaylist] = useState<string | string[]>([]);
<playlistContext.Provider value={{
playlist,
setPlaylist: (songID: string[]) => setPlaylist(songID),
}}>
<Navigation colorScheme={colorScheme} />
<StatusBar />
<PlayerWidget />
</playlistContext.Provider>

相关内容

  • 没有找到相关文章

最新更新