admin管理员组文章数量:1304019
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/
...ponents/
........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 './ponents/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 './ponents/ComponentB';
class ComponentA extends Component {
render() {
return (
<div className="ponentAStyle">
<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 pile
./src/ponents/ComponentA.jsx
Module not found: Can't resolve './styles/ComponentA.css' in 'C:\xampp\htdocs\custom-k39\src\ponents'
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 pile
./src/ponents/ComponentA.jsx
Module not found: Can't resolve './ponents/ComponentB' in 'C:\xampp\htdocs\custom-k39\src\ponents'
This error occurred during the build time and cannot be dismissed.
How can I import another ponent from another ponent and its respective css?
Thanks.
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/
....ponents/
........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 './ponents/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 './ponents/ComponentB';
class ComponentA extends Component {
render() {
return (
<div className="ponentAStyle">
<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 pile
./src/ponents/ComponentA.jsx
Module not found: Can't resolve './styles/ComponentA.css' in 'C:\xampp\htdocs\custom-k39\src\ponents'
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 pile
./src/ponents/ComponentA.jsx
Module not found: Can't resolve './ponents/ComponentB' in 'C:\xampp\htdocs\custom-k39\src\ponents'
This error occurred during the build time and cannot be dismissed.
How can I import another ponent from another ponent and its respective css?
Thanks.
Share Improve this question asked Jul 9, 2017 at 18:27 Walter DeviaWalter Devia 651 gold badge1 silver badge7 bronze badges1 Answer
Reset to default 5You 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./edit/#!/trashy-unicorn.
(Click on Show Live in top left hand corner.)
Currently what is happening with your error is
import ComponentB from './ponents/ComponentB';
is is looking in the path
src/ponents/ponents/ComponentB
which does not exist and so gives you an error. Same thing is happening with your styles.
Hope this helps.
本文标签: javascriptImport css files and react components in a createreactapp applicationStack Overflow
版权声明:本文标题:javascript - Import css files and react components in a create-react-app application? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741717124a2394179.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论