i have a trouble importing .css stylesheet on a react basic app, I have this components:
AvatarHeader.js:
JavaScript
x
9
1
import styles from './AvatarHeader.css';
2
3
export default function AvatarHeader({ style, children }) {
4
return (
5
<div>
6
<img style={styles.image} src={Background} alt="background">
7
{ children }
8
</img>
9
AvatarHeader.css:
JavaScript
1
4
1
.image {
2
width: 400
3
}
4
AvatarHeader.js & AvatarHeader.css are in the same folder.
package.json:
JavaScript
1
26
26
1
{
2
"name": "UtWeb",
3
"version": "0.1.0",
4
"private": true,
5
"devDependencies": {
6
"css-loader": "^0.25.0",
7
"react-scripts": "0.9.0",
8
"webpack": "^2.2.1",
9
"webpack-dev-server": "^1.9.0"
10
},
11
"dependencies": {
12
"react": "^15.4.2",
13
"react-dom": "^15.4.2",
14
"react-redux": "^4.3.0",
15
"react-router": "^2.0.0",
16
"redux": "^3.2.1",
17
"react-router-redux": "^4.0.0"
18
},
19
"scripts": {
20
"start": "react-scripts start",
21
"build": "react-scripts build",
22
"test": "react-scripts test --env=jsdom",
23
"eject": "react-scripts eject"
24
}
25
}
26
Styles aren’t applied on the component, but it’s works if I rewrite AvatarHeader.js so:
JavaScript
1
13
13
1
import styles from './AvatarHeader.css';
2
3
const image = {
4
width: 400
5
}
6
7
export default function AvatarHeader({ style, children }) {
8
return (
9
<div>
10
<img style={image} src={Background} alt="background">
11
{ children }
12
</img>
13
I don’t understand how to fix this bug for use the first way to import css file.
Advertisement
Answer
JavaScript
1
3
1
import './AvatarHeader.css';`
2
<img className='image' />`
3
is the correct syntax