简体   繁体   English

Ant-Design CSS 加载不正确

[英]Ant-Design CSS not loading properly

I am having an issue with rendering my Ant-Design CSS on the first-render using React.js.我在使用 React.js 在第一次渲染时渲染我的 Ant-Design CSS 时遇到问题。 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:我正在尝试根据文档通过 config-overrides.js 文件导入 Ant-Design 模块:

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

Here is my index.js file:这是我的 index.js 文件:

 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:这是我的 App.js 和 App.css 以供更多参考:

 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:这是显示问题的两个图像: 图像一 - 第一次渲染

And here is the page after the second render:这是第二次渲染后的页面: 图像二 - 第二次渲染

Just import this file in your jsx file or js file:只需在您的 jsx 文件或 js 文件中导入此文件:

If you import it in App.jsx file once then no need to import in other files如果您在 App.jsx 文件中导入一次,则无需在其他文件中导入

import "antd/dist/antd.css";

this below import is used my react project with create-react-app cli下面的导入用于我的 react 项目和 create-react-app cli
import 'antd/dist/antd.css', use this import to your root component.导入'antd/dist/antd.css',使用这个导入到你的根组件。

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

To the top of either/both of App.css and Index.css.到 App.css 和 Index.css 之一/两者的顶部。

Hope that helps!希望有帮助! 👍. 👍。

PS - If you are using a single component for instance lets say Input, then import only that part. PS - 例如,如果您使用单个组件,请说输入,然后仅导入该部分。

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

不要从蚂蚁导入根的风格,因为它们包含一些全球性的风格,遗憾的是,会影响你的造型,无论如何,这是解决了很多次他们,但我仍然发现的唯一的解决办法是直接导入造型像组件这(用你的组件替换select ): import "antd/lib/select/style/index.css";

在您的 index.js 文件中,您可以导入 ant 样式文件:

import 'antd/dist/antd.css';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM