Unfortunately, you cannot import/require components dynamically in React environment.

Depending on how many buildings/blueprints there are, it's possible to import them one by one, create component-building map and pick component by building ID.

If there are many/infinite components to load, I would surely pick another method - don't know content of your problem.

import BlueprintA from './BlueprintA'
import BlueprintB from './BlueprintB'
import BlueprintC from './BlueprintC'
// ...

class BuildingMap extends React.Component {
  render(){
    const C = {
      buildingA: BlueprintA,
      buildingB: BlueprintB,
      buildingC: BlueprintC,
      // ...
    }[this.props.building]

    return (
      <div className="blueprint" id={this.props.building}>
         <C />
      </div>
    )
  }
}
Answer from Andreyco on Stack Overflow
🌐
Reddit
reddit.com › r/learnreactjs › how do i import a variable from one component to another
r/learnreactjs on Reddit: How do i import a variable from one component to another
April 3, 2023 -

Hi guys, ive been struggling to import a variable from component dashboard.jsx to index.jsx. Im basically trying to upload an image to a folder and im using multer to do the job for me. heres the code that creates the file name:

Dashboard.jsx

const handleSubmit = (event) => {
event.preventDefault();
const formData = new FormData(); formData.append('image', file);
///// this is where the file name is created 
let stallimage = Date.now() + '-' + Math.round(Math.random() * 1E9);
axios.post('http://localhost:3001/upload', formData)         
.then(function (response) { console.log(response);         
})         
.catch(function (error) { console.log(error);         
});     
}

Index.js

const multer = require('multer');
const path = require('path');
const storage = multer.diskStorage({ 
destination: function (req, file, cb) { cb(null, '../Images/StallImages/')   
}, 
filename: function (req, file, cb) { 
////// where the name needs to be imported into 
const uniqueSuffix =  Date.now() + '-' + Math.round(Math.random() * 1E9) + path.extname(file.originalname) cb(null, uniqueSuffix)   
} 
})

ive been stuck on this for a really long time and its really getting to me. I basically want the same result of variable stallimage to called by variable uniquesuffix. ANY HELP GREATLY APPRECIATED

🌐
Velog
velog.io › @_jouz_ryul › Exports-Imports-Modules-in-React
Exports & Imports Modules in React
// thats why variables are wrapped with brackets ... import { smth } from 'utility.js' // same as above import { smth as Smithh } from 'utility.js' // now js will indicate Smithh as smth from utility.js import * as bundled from 'utility.js' // now js will indicate * as bundled from utility.js // for this case it is useful when whatever variable like to be imported is a object // and we would like to approach to values in deep level while variable which assigned as object // has a name with long length such as // const userDataForInsuranceWithPreminum = {} // now we can approach like bundled.key.anotherKey
🌐
Malcolmkee
malcolmkee.com › js-the-react-parts › es-module
ES Module - JavaScript: The React Parts - Malcolm Kee
To export variables/function from ... // y can be accessed by other files. To import a variable from another file, you use the keyword import....
🌐
Reddit
reddit.com › r/reactjs › noob react question: trying to import/export variable between files
r/reactjs on Reddit: Noob react question: trying to import/export variable between files
June 16, 2022 -

I have two jsx files, a.jsx and b.jsx.

In a.jsx I have a variable that I calculate on page load. I want to export this variable so that it can be referenced in b.jsx.

I'm thinking I'll need to do something like this in a.jsx: "export var value" and something like this in b.jsx: "important value from ./a"

This is a simple example, but it's exactly what I am trying to accomplish. 'value' is always undefined in b.jsx currently. What am I doing wrong?

Thanks so much in advance.

🌐
React
react.dev › learn › importing-and-exporting-components
Importing and Exporting Components – React
Import it in the file where you’ll use the component (using the corresponding technique for importing default or named exports).
🌐
Medium
sohamnakhare.medium.com › import-react-component-using-a-variable-path-94d2f1492d40
Import react component using a variable path | by Soham Nakhare | Medium
December 13, 2023 - The import() must contain at least some information about where the module is located. The solution for this issue is straightforward. Simply specify the directory to be scanned, and that should resolve it. ... Webpack configuration can be intricate and may appear mystifying at times, but it is a highly potent tool equipped with numerous features. Combining React lazy loading and Webpack dynamic import expression offers a robust solution for dynamically loading components, especially when dealing with a substantial number of variants.
🌐
Reddit
reddit.com › r/reactjs › dynamic import with variable path
r/reactjs on Reddit: Dynamic import with variable path
November 25, 2021 -

I'm looking for a way to dynamically import a component from a path including a variable, and display a fallback component. Currently I'm doing this with an intermediary component:

const DynamicComponent = ({ element, elementName }) => {
    
    const [component, setComponent] = useState(element)

    useEffect(() => {
        import(`/components/${element.type.name}`)
            .then(module => {
                setComponent(module.default)
            })
            .catch(console.log)
        }, [element])

    return component
}

Which is then used like this:

<DynamicComponent element={<SomeComponent />} elementName="SomeComponent" />

The elementName prop is required because in production the function names are minified so I can't get the component name from anywhere else.

This seems to work well locally, but is pretty flaky in production - it errors far more often, and sometimes doesn't show the right component which appears to be related to how and when the dynamic path is generated.

Can anyone recommend a cleaner/better/simpler solution?

Thanks!

🌐
Medium
medium.com › nerd-for-tech › get-global-variables-in-react-js-490cf68f2a73
Get global variables in React JS
August 10, 2021 - Now that the Context was exported, it's time to import it into the components. First, go to your App.js file and wrap all the components you want to access the context. All child components will automatically inherit the context. import React, ...
Find elsewhere
🌐
GitHub
github.com › react-boilerplate › react-boilerplate › issues › 535
How to import a single variables file ? · Issue #535 · react-boilerplate/react-boilerplate
June 12, 2016 - react-boilerplate / react-boilerplate Public template · Notifications · You must be signed in to change notification settings · Fork 6.1k · Star 29.5k · New issueCopy link · New issueCopy link · Closed · Closed · How to import a single variables file ?#535 ·
Author   suhaotian
🌐
React Native
reactnative.dev › docs › intro-react
React Fundamentals · React Native
February 20, 2026 - Because JSX is JavaScript, you can use variables inside it. Here you are declaring a name for the cat, name, and embedding it with curly braces inside <Text>. Any JavaScript expression will work between curly braces, including function calls like {getFullName("Rum", "Tum", "Tugger")}: ... You can think of curly braces as creating a portal into JS functionality in your JSX! ... Because JSX is included in the React library, it won’t work if you don’t have import React from 'react' at the top of your file!
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs-importing-exporting
React Importing and Exporting Components | GeeksforGeeks
import React from "react"; const MyComponent = () => { return <h1>Hello from MyComponent!</h1>; }; export default MyComponent; ... Default Export: Exports one thing from a file (like a component). Default Import: Imports the default export from another file, naming it as needed. Named exports allow you to export multiple components or variables from a single file.
Published   March 21, 2025
🌐
Josh W. Comeau
joshwcomeau.com › css › css-variables-for-react-devs
How to use CSS variables with React • Josh W. Comeau
April 13, 2020 - If you'd like, you can keep storing them in a constants.js file. They'll be used to instantiate the CSS variables, but then also imported wherever you need the raw values in JS:
🌐
Medium
medium.com › nerd-for-tech › how-imports-work-in-react-121e3741fb70
How imports work in React. My understanding of the module system… | by Kawere Wagaba | Nerd For Tech | Medium
May 23, 2021 - The mental modal is that it gets everything exposed by exports.<API>, combines it into one object, and assigns that object to a variable named React — or any other name. The result of the above two methods is that all the APIs now are properties on the React object and can be accessed via React.<API>, for example class <ComponentName> extends React.Component {}. For convenience, you can import different APIs separately (or as a shortcut) — kinda like the exports vs module.exports scenario, as shown below:
🌐
Medium
medium.com › @Carmichaelize › react-importing-global-variables-from-the-window-object-b7db6665651d
React — Importing Global Variables from the Window Object | by Scott Carmichael | Medium
November 3, 2017 - This allows the variable to be used as a module more in keeping with the ES6 approach and can be imported into project files when needed. The benefit of this approach is not having to rewrite (or replace) code that utilises the window object in order to maintain an ES6 coding standard in the project. You can import it and use it like you would a custom component or service class. Its also a useful way to get around stricter React CLIs and compilers throwing errors when using undeclared global variables.