in Education by
I have a simple components: const Sidebar = (props) => { ... } const SidebarLink = (props) => { ... } To avoid bloat in imports I want to do simple namespacing: Sidebar.propTypes = propTypes Sidebar.Link = SidebarLink export default Sidebar And use it like: ... ... ... Question: Are there any cons for this approach? Can somethings go wrong? Is it fine as a whole? Thanks JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I have used this approach quite often when I was earliest stage of learning React. Here's how I did: export const Sidebar = (props) => { ... } export const SidebarLink = (props) => { ... } And when we needed to use them: import * as Sidebar from '...' // path to the exports ... ... ... But it looks a little uglier in your case. Here's what I'd have used this approach: (Just an example) When I used this approach exactly? I used this approach whenever I need to use several components: // ComponentOne.js export const ComponentOne = () => {} // ComponentTwo.js export const ComponentTwo = () => {} // ComponentThree.js export const ComponentThree = () => {} // ... // ComponentIndex.js export { ComponentOne } from 'ComponentOne' export { ComponentTwo } from 'ComponentTwo' // ... And now using it in separate component. So, it's be better to import all rather than using import statement a huge number of lines: import * as ComponentAll from 'ComponentIndex' ... But obviously, there's one big con while using this approach, this will impact on performance. Continue reading below to see it why: You'll be importing all of the components even though, you'll not be using some component. For eg.: // PlaceOne.js // PlaceTwo.js So, you're importing all exported components in the PlaceOne.js and PlaceTwo.js. To avoid this con, use import statement like: import { ComponentOne, ComponentTwo, // ... import only required component } from 'ComponentIndex' // Now, you can simply use: This concludes that using rather than is better approach. And also, we will not loose code splitting advantage while using approach.

Related questions

0 votes
    This is part of the component : import MyComp from '../../lib/MyComp' const Data = ( { data } ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    This is part of the component : import MyComp from '../../lib/MyComp' const Data = ( { data } ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm trying to create an Angular reusable button component. Right now, it is used like so: [label] ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    I am trying to use ES2017 async/await syntax with Babel. In package.json, I have "babel": { " ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 6, 2022 in Education by JackTerrance
0 votes
    There are loads of questions about the current date in JavaScript in general, but in my scenario, I have ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 3, 2022 in Education by JackTerrance
0 votes
    Course Results (${courseResponseList.getCourses().size()}) Want to show above div. jquery script. jQuery. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I am using an array in order to calculate large powers of 2. The arrays add to each other and ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    I have a simple WCF service that I'm exposing using a .svc. The service has some related DataContract ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 11, 2022 in Education by JackTerrance
0 votes
    How can I get the td values with jquery? The while loops will generate a few with the same class ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Different JavaScript Questions and Answers...
asked Sep 17, 2022 in Technology by Editorial Staff
0 votes
    I've created a getSpectrum method using the getByteFrequencyData method on the Web Audio API's Analyser Node ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    Have jquery dialog as closeOnescape as false. want to trigger an event based on esc key press how do ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...