Home > Mobile >  how to paste a segment of code from another file?
how to paste a segment of code from another file?

Time:09-27

This might be a weird question to ask but is there a way to paste/import a segment of code from another file?
for example:

file1.js:

export default function App() {
  return (
   some_paste_function('file2.js');
  );
}

file2.js:

<View>
       <View>
         <Text>Hello World</Text>
      </View>
</View>

basically the some_paste_function(file_name); just pastes the content of the file2.js in the functions place, it's more for making the file system look cleaner rather than using import

is there such a function or anything similar to it in react native or even js in general?

CodePudding user response:

Technically you can use require to dynamically load contents from another file, if they are exported from it, and only if you're running on a module loader that uses it.

export default function App() {
  return require("./file2.js");
}
export default function Component() {
  return <View>
    {/* ... */}
  </View>;
}

However, I'd strongly advise against it, as you lose the benefits of proper type checking, auto-refactoring and your dependancy graph gets messy.

  • Related