Skip to content

Instantly share code, notes, and snippets.

@wanmigs
Last active April 15, 2019 11:12
Show Gist options
  • Save wanmigs/60667386a6a6a7ca41018c9967938d6f to your computer and use it in GitHub Desktop.
Save wanmigs/60667386a6a6a7ca41018c9967938d6f to your computer and use it in GitHub Desktop.
React Native Dynamic Exports
const requireContext = require.context('./', false, /.*\.(js)$/)
let components = {};
requireContext.keys().forEach(fileName => {
if (fileName === './index.js') return
let name = fileName.replace(/(\.\/|\.js)/g, '')
components[name] = requireContext(fileName).default
})
module.exports = components
@wanmigs
Copy link
Author

wanmigs commented Mar 18, 2019

export base components under the same folder?

js 
components/
  base/
    TextInput.js
    Button.js
    index.js <-- this would be our code

instead of importing it like this

import TextInput from '~/components/base/TextInput'
import Button from '~/components/base/Button'

You can do it like this:

import { TextInput, Button } from '~/components/base/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment