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
206 views
in Technique[技术] by (71.8m points)

javascript - Correctly importing components in react native?

Currently I am exporting a component

  export default function ChooseNamespace() {


  return (
    <View>
      <Text>HELLO</Text>
    </View>
  );
}

And importing it here:

import ChooseNamespace from "./screens/ChooseNamespace";

   const AppDrawerNavigator = () => {
    console.log(storeState.userNamespace);
    if (storeState.userNamespace === null) {
      return <ChooseNamespace />;
    } else {
      return (
        <AppDrawer.Navigator
          drawerContent={(props) => <AppDrawerContent {...props} />}
          drawerContentOptions={{
            activeTintColor: "blue",
            labelStyle: {
              color: "white",
            },
          }}
        >
          <AppDrawer.Screen
            name="Chat"
            children={(props) => <ChatScreen name="Blindly Chat" {...props} />}
          />
          <AppDrawer.Screen
            name="Profile"
            children={(props) => <ProfileScreen name="Profile" {...props} />}
          />
          <AppDrawer.Screen
            name="DirectMessages"
            children={(props) => <DMScreen {...props} />}
          />
          <AppDrawer.Screen
            name="Threads"
            children={(props) => <ThreadsScreen {...props} />}
          />
          <AppDrawer.Screen
            name="Settings"
            children={(props) => <SettingsScreen {...props} />}
          />
          <AppDrawer.Screen
            name="Support"
            children={(props) => <SupportScreen {...props} />}
          />
        </AppDrawer.Navigator>
      );
    }
  };

Which is then throwing the error

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Check the render method of ChooseNamespace.

Can someone try an explain the issue here? I am having a bit of trouble finding it.

question from:https://stackoverflow.com/questions/65617280/correctly-importing-components-in-react-native

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

1 Reply

0 votes
by (71.8m points)
const ChooseNamespace =()=> {


  return (
    <View>
      <Text>HELLO</Text>
    </View>
  );
}
export default ChooseNamespace

Try this code. ES6 doesn't allow export default const. You must declare the constant first then export it. Or if WANT to have a function then do this:

function ChooseNamespace() {


  return (
    <View>
      <Text>HELLO</Text>
    </View>
  );
}

export default ChooseNamespace;

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

...