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 OverflowUnfortunately, 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>
)
}
}
This question is pretty old but as I was looking for how to solve the same problem let me give my answer. It can be done with dynamic import React.lazy:
const OtherComponent = React.lazy(() => import('./OtherComponent'));
See more details here: https://reactjs.org/docs/code-splitting.html#reactlazy
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
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.
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!
Edit JS-fiddle here https://jsfiddle.net/xzehg1by/9/
You can create Refs and access state and methods from it. Something like this.
constructor() {
this.myRef = React.createRef();
}
render() { ... <MyModule id='themodule' ref={this.myRef} /> }
printValues() {
console.log(this.myRef)
}
more info here https://reactjs.org/docs/refs-and-the-dom.html
Basically, your state (selectedValues) has to go one level up in the React tree. You have to declare it as App's state, and then pass it down to MyModule via props.
Btw in addValue(), you're not changing any state. And this.selectedValues will be undefined. It's this.state.selectedValues, and this.props.selectedValues once you correct your code.
If you are doing this:
export const navLinks = [
{
name: "home",
href: "/"
},
{
name: "subs",
href: "/subs"
}
];
then you are importing it wrong. Then do this:
import {navLinks} from "./components/nav-links";
Note this: ./components/nav-links you forgot ./ in import statement
If you dont want to use {} the add default statement in your export like this:
const navLinks = [
{
name: "home",
href: "/"
},
{
name: "subs",
href: "/subs"
}
];
export default navLinks;
You're missing to export the nav links as default :
import React from 'react';
const navLinks = [
{
name: "home",
href: "/"
},
{
name: "subs",
href: "/subs"
}
];
export default navLinks;
and add dot . in import navLinks from "./components/nav-links";