Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Ant Design styles not loading

Tags:

reactjs

antd

I'm using webpack 4x and trying to include antd along with babel-plugin-import. I've updated my webpack config to use:

    {
      test: /\.less$/,
      include: [/[\\/]node_modules[\\/].*antd/],
      use: [
        'css-loader',
        {
          loader: 'less-loader',
          options: {
            modifyVars: { '@primary-color': '#1DA57A' },
            javascriptEnabled: true
          }
        }
      ]
    },

This works, as in it doesn't throw any errors. However, the styles don't seem to be showing up in the actual app. I'm including like this:

import { Button } from 'antd'

class App extends Component {
  render() {
    return <Button type="primary">Button</Button>
  }
}

It's rendering the button just fine, it's just the styles that aren't coming in. Any idea if there's something else that needs to be done so the styles are actually included? Thank you!

like image 585
dzm Avatar asked Oct 03 '18 19:10

dzm


People also ask

How do I import an antd style?

Click the "Open in Editor" icon in the first example to open an editor with source code to use out-of-the-box. Now you can import the Alert component into the codesandbox: - import { DatePicker, message } from 'antd'; + import { DatePicker, message, Alert } from 'antd';

Which is better ant design or material UI?

Material UI offers a large variety of built-in theme options whereas Ant Design doesn't offer as many of that. Material UI is only suitable for Android apps but Ant Design is compatible with iOS, Android, and web.

How do I customize my antd components?

If we decide to customize another component in the project, even a widely used one, we will just need to add a file with the wrapper and change the path of that component in the file with re-exports located at src/components/antd/index.

Is antd Pro free?

ant-design-pro - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers.


1 Answers

you need to import the less file of antd in less :

//main.less
@import "~antd/dist/antd.less";

then import this file in ts/js index:

import './less/main.less';

you can also add the theme config file in main.less

This is the webpack config I use (webpack3):

,{
                test: /\.less$/,
                use: [{
                    loader: 'file-loader',
                    options: {
                        name: 'theme.css'
                    }
                },{
                    loader: 'less-loader',
                    options: { javascriptEnabled: true }// compiles Less to CSS
                }]
          },
like image 94
C Taque Avatar answered Oct 21 '22 02:10

C Taque