javascript - Importing css in JSX file - Stack Overflow

I am trying to import css styles into js file, but i cannot use styles properlyin my file Graph.js: imp

I am trying to import css styles into js file, but i cannot use styles properly

in my file Graph.js:

import React from 'react';
import styles from './Graph.css';
import Center from './Center/Center'

class Graph extends React.Component {

    render(){
        return(
            <div className={styles.graph}>
                <Center>test</Center>
            </div>
        );
    }
}

export default Graph;

I try to use style from file Graph.css which looks like this:

.graph {
    height: 500px;
    width: 500px;
    border: 1px solid grey;
}

Is there a possibility to manage importing styles to ponents this way?

I am trying to import css styles into js file, but i cannot use styles properly

in my file Graph.js:

import React from 'react';
import styles from './Graph.css';
import Center from './Center/Center'

class Graph extends React.Component {

    render(){
        return(
            <div className={styles.graph}>
                <Center>test</Center>
            </div>
        );
    }
}

export default Graph;

I try to use style from file Graph.css which looks like this:

.graph {
    height: 500px;
    width: 500px;
    border: 1px solid grey;
}

Is there a possibility to manage importing styles to ponents this way?

Share Improve this question edited Jun 7, 2019 at 9:26 user5683940 asked Jun 7, 2019 at 8:23 Ján BáťkaJán Báťka 811 gold badge2 silver badges5 bronze badges 1
  • without bundler - no. Use webpack or rollout to manage it – Evgeniy Commented Jun 7, 2019 at 8:29
Add a ment  | 

4 Answers 4

Reset to default 4

your import statement should be import './Graph.css'; and in ponent <div className="graph">

You are probably confusing react and react-native.

In react, you don't need to styles.graph.

Rather doing the way you do it in normal .html file would work

import React from 'react';
import styles from './Graph.css';
import Center from './Center/Center'

class Graph extends React.Component {
    render() {
        return (
            <div className="graph"
                <Center>test</Center>
            </div >
        );
    }
}

export default Graph;

You need to import file directly i.e import './Graph.css' and use it as normal class on div or whenever you want. There are other ways to stlye element such as inline style and more. You can explore more here . https://codeburst.io/4-four-ways-to-style-react-ponents-ac6f323da822 (its great article)

You are missing an important css rule option in your webpack config.

{
    loader: 'css-loader',
    options: {
        modules: true,
        camelCase: true
    }
}

Add this options object in order to import styles. The modules option let you enable to use your css as a style object and camelCase option enables using "some-class" as someClass.

Hope this will help.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745132645a4613046.html

相关推荐

  • javascript - Importing css in JSX file - Stack Overflow

    I am trying to import css styles into js file, but i cannot use styles properlyin my file Graph.js: imp

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信