0

I have to import multiple files in my App.js . My folder structure is

/src  
   /components       
     layout.js
   App.js  
   index.css
   index.js

Here i want to import layout.js file from App.js. My code is

import React from 'react';
import Layout from './components/layout';

class App extends React.Component{
   render(){
       return ( 
          <div>
              <p>Hi</p>
          </div>
      );
   }
 }

 export default App;

In my code, how to import layout.js file ?

3
  • 2
    import Layout from './components/layout'; Commented Jan 12, 2018 at 6:20
  • you have already imported it import Layout from './components/layout'; Commented Jan 12, 2018 at 6:25
  • but its not working Commented Jan 12, 2018 at 6:34

3 Answers 3

1

Ideally, your layout is a component which you can simply import into your main. One good pratcise when creating new component is to create a separate folder inside components folder and create index.js. By doing so you can import components like below:

/src
/components
  /layout
    index.js
App.js
index.css
index.js



import React from 'react';
import Layout from './components/layout';

class App extends React.Component{
   render(){
       return ( 
          <div>
              <p>Hi</p>
              <Layout/>
          </div>
      );
   }
 }

 export default App;
Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you imported Layout correctly. If it is not working, perhaps you forgot to export default Layout in layout.js?

Comments

0

You need to use webpack's resolve.extensions to import file paths that don't end in .js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.