简体   繁体   中英

React Native add Screens dynamically

I want to add screens dynamically from a changeable array.

This works: 在此处输入图片说明

but this doesn't work: 在此处输入图片说明 It says: Error: Couldn't find any screens for the navigator. Have you defined any screens as its children?

How can I fix the second to be more "dynamic"?

you can change your components to list of objects.

then loop inside the item by their key and value.

const components = [
 {
   name: "Home",
   element: HomeScreen
 },
 {
   name: "Details",
   element: DetailsScreen
 }
];

const renderScreen = () => {
  let result = null;

  if (components) {
    result = components.map((item, i) => {
       return <Stack.Screen name={item.name} element={item.element} />
    });
  }

  return result;
}

return(
  {renderScreen()}
)

element => { <Stack... /> }改为element => { return <Stack... /> }element => ( <Stack... /> )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM