Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ant-Design CSS not loading properly

I am having an issue with rendering my Ant-Design CSS on the first-render using React.js. I have a very basic page, that is just rendering a button.

import React from 'react';

import { Button } from 'antd';

const LoginPage = () => {
    return (
        <div>
            <Button type="primary">Button</Button>
        </div>
    )
};

export default LoginPage;

I am trying to import the Ant-Design modules through the config-overrides.js file, as per the documentation:

const { override, fixBabelImports } = require('customize-cra');

module.exports = override(
    fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        style: 'css',
    }),
);

Here is my index.js file:

import { Provider } from 'react-redux';
import thunkMiddleware from 'redux-thunk';
import { createStore, applyMiddleware } from 'redux';

import 'normalize.css';

import App from './components/App/App';
import reducers from './reducers';
import { fetchUser } from './actions';

import * as serviceWorker from './serviceWorker';

const store = createStore(reducers, applyMiddleware(thunkMiddleware));

store.dispatch(fetchUser()).then(() => console.log(store.getState()));


ReactDOM.render(
    <Provider store={store}>
        <App />
    </Provider>,
    document.getElementById('root')
);

serviceWorker.unregister();

And here is my App.js and App.css for more reference:

import React, { Component } from 'react';
import LoginPage from '../LoginPage/LoginPage';
import DashboardPage from '../DashboardPage/DashboardPage';

import { Spin } from 'antd';

import './App.css';

import { connect } from 'react-redux';

class App extends Component {

    constructor(props){
        super(props);
        this.state = {
            loggedIn: false
        }
    }

    componentDidMount(){
        this.setState({loggedIn: true });
    }

    render() {
       return <LoginPage/>
    }
}

const mapStateToProps = (state) => {
    console.log(state);
    return {
        user: state.currUser
    };
};

export default connect(mapStateToProps)(App);

@import '~antd/dist/antd.css';

However, on the first render it will only show a normal button, before fixing itself a second later. Here are two images that show the problem: Image One - First Render

And here is the page after the second render: Image Two - Second Render

like image 840
DrummerGenius Avatar asked Feb 17 '19 18:02

DrummerGenius


5 Answers

Just import this file in your jsx file or js file:

If you import it in App.jsx file once then no need to import in other files

import "antd/dist/antd.css";
like image 131
Usama Majid Avatar answered Oct 24 '22 02:10

Usama Majid


this below import is used my react project with create-react-app cli
import 'antd/dist/antd.css', use this import to your root component.

like image 26
sathish kumar Avatar answered Oct 24 '22 04:10

sathish kumar


Add @import '~antd/dist/antd.css';

To the top of either/both of App.css and Index.css.

Hope that helps! 👍.

P.S - If you are using a single component for instance lets say Input, then import only that part.

import 'antd/es/input/style/index.css';

like image 39
Divyanshu Rawat Avatar answered Oct 24 '22 03:10

Divyanshu Rawat


Do NOT import the root styles from ant, as they contain some global styles, unfortunately, and will affect yours styling anyway, this was addressed a lot of times to them, but still I found the only solution is to directly import the components styling like this (replace select with your component): import "antd/lib/select/style/index.css";

like image 45
Andrew Katsewich Avatar answered Oct 24 '22 04:10

Andrew Katsewich


In your index.js file, you can import ant style files:

import 'antd/dist/antd.css';
like image 3
Md. Tarikul Islam Soikot Avatar answered Oct 24 '22 04:10

Md. Tarikul Islam Soikot