简体   繁体   中英

how to paste a segment of code from another file?

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?

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.

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