I'm very new to React so please excuse my ignorance. I basically want to know the "way" to export multiple ReactJS objects from directories for larger codebases and best practices. This is not really in the ReactJS tutorial.
Let's say I have a file ./main.js
and I want to import a lib (from a dir because I might have other ones).
Let's say I have a library in a dir ./library_dir/my_lib.js
and I want to use it in ./main.js
like so.
In ./main.js
, I might want to use this lib and initialize some ExampleComponent
like this:
var my_lib = require('./library_dir/my_lib.js');
ReactDOM.render(
(<BrowserRouter>
<Switch>
<Route exact path="/component_route" component={my_lib.ExampleComponent}>
</Switch>
</BrowserRouter>)
, document.getElementById("root"));
So in ./library_dir/my_lib.js
, I want to be able to export MULTIPLE reactjs classes. How would I do this?
Would I want to use export default ....
or module.exports
? Which one and WHY? I've read that if I import react
in this lib, I can't do a module.exports
.
Can someone give me an example where in ./library_dir/my_lib.js
, I would be able to export multiple react classes that would look something like this?
import React from 'react';
my_lib = {
test_function: function() {
alert("TEST FUNCTION CALLED");
},
// How would I do this bellow?
ExampleComonent: class ExampleComponent extends React.Component {
constructor(props) {
super(props);
// doing all thing things...
}
render() {
return (
<h1>This is ExampleComponent.</h1>
);
}
}
// I need to be able to export multiple components, so here's another one.
ExampleComonentTwo: class ExampleComponentTwo extends React.Component {
constructor(props) {
super(props);
// doing all thing things...
}
render() {
return (
<h1>This is ExampleComponentTwo.</h1>
);
}
}
}
export default my_lib;