5

I'm just starting with react.js and create-react-app application so excuse me if this is an obvious question.

My 'src' folder structure is like this:

scr/
....components/
........ComponentA.jsx
........Componentb.jsx
....styles/
........ComponentA.css
....App.css
....App.js
....App.test.js
....index.css
....index.js
...OTHER DEFAULT FILES

In App.js I have this:

import React, { Component } from 'react';
import ComponentA from './components/ComponentA';

class App extends Component {
  render() {
    return (
      <div className="App">
        <ComponentA />
      </div>
    );
  }
}

export default App;

In ComponentA I have this:

import React, { Component } from 'react';
import './styles/ComponentA.css';
import ComponentB from './components/ComponentB';

class ComponentA extends Component {
  render() {
    return (
      <div className="componentAStyle">
        <ComponentB />
      </div>
    );
  }
}

export default ComponentA;

In ComponentB I have this:

import React, { Component } from 'react';

class ComponentB extends Component {
  render() {
    return (
      <div>
        <h1>Hello from ComponentB</h1>
      </div>
    );
  }
}

export default ComponentB;

What I'm trying to do is just import ComponentB from ComponentA and also import the style for ComponentA but all this fall showing the following message:

Failed to compile
./src/components/ComponentA.jsx
Module not found: Can't resolve './styles/ComponentA.css' in 'C:\xampp\htdocs\custom-k39\src\components'
This error occurred during the build time and cannot be dismissed.

And if I remove the css import there is another error message:

Failed to compile
./src/components/ComponentA.jsx
Module not found: Can't resolve './components/ComponentB' in 'C:\xampp\htdocs\custom-k39\src\components'
This error occurred during the build time and cannot be dismissed.

How can I import another component from another component and its respective css?

Thanks.

1 Answer 1

5

You need to make the paths you are importing are relative to the current location of the file doing the import.

The correct imports would be

 Component A

import React, { Component } from 'react';
import '../styles/ComponentA.css';
import ComponentB from './ComponentB';

You can see this working at https://glitch.com/edit/#!/trashy-unicorn.
(Click on Show Live in top left hand corner.)

Currently what is happening with your error is

import ComponentB from './components/ComponentB';

is is looking in the path

src/components/components/ComponentB

which does not exist and so gives you an error. Same thing is happening with your styles.

Hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It helped me. But now how do you get positioned in 'src/' folder? e.g. How do you import a module located in root of 'src' folder from a component located in 'src/folderA/folderB/folderC/.../Component.jsx' ?
you can go back one folder level with '../' This is what happened with import '../styles/ComponentA.css'; It went back one level and then went to styles folder. you can go back two folders with '../../' and so on. You'll need to count how far deep you are from your src folder and then keep adding '../'s until you get there.

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.