zoukankan      html  css  js  c++  java
  • react如何配置antd3.0版本

    官网:https://ant.design/docs/react/use-with-create-react-app-cn

    1、安装:antd

    npm install antd@^3.26.13 -S
    

     

    2.按需引入antd, 安装 npm add react-app-rewired customize-cra

    /* package.json */
    "scripts": {
    -   "start": "react-scripts start",
    +   "start": "react-app-rewired start",
    -   "build": "react-scripts build",
    +   "build": "react-app-rewired build",
    -   "test": "react-scripts test",
    +   "test": "react-app-rewired test",
    }
    

      

    3.根目录下创建config-overrides.js

    const { 
      addWebpackAlias, 
      addLessLoader, 
      fixBabelImports, 
      override, 
      addDecoratorsLegacy 
    } = require('customize-cra')
    const path = require('path')
    
    module.exports = override(
      // @ 修饰器
      addDecoratorsLegacy(),
      fixBabelImports('import', {
        libraryName: 'antd',
        libraryDirectory: 'es',
        // 支持 less sass stylus
        style: true,
      }),
      // 支持 antd 主题定制
      addLessLoader({
        javascriptEnabled: true,
      }),
      // 别名
      addWebpackAlias({
        '@': path.resolve(__dirname, 'src'),  
        '@@': path.resolve(__dirname, 'src/components'),
      })
    )
    

      

    这样就可以正常使用了。下面举个form的例子

    import React from 'react'
    import { Form, Input, Button, } from 'antd';
    
    //export default @Form.create()    //使用@修饰器
    export default @Form.create({    //这里就顺便把表单回填也写下算了。 
      mapPropsToFields(props) {
        return {
          username: Form.createFormField({
            value: 22,  //
          })
        }
      }
    })
    class NormalLoginForm extends React.Component {
      handleSubmit = e => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
          if (!err) {
            console.log('Received values of form: ', values);
          }
        });
      };
    
      render() {
        const { getFieldDecorator } = this.props.form;
        return (
          <Form onSubmit={this.handleSubmit} className="login-form">
            <Form.Item>
              {getFieldDecorator('username', {
                rules: [{ required: true, message: 'Please input your username!' }],
              })(
                <Input placeholder="Username" />,
              )}
            </Form.Item>
            <Form.Item>
              <Button type="primary" htmlType="submit" className="login-form-button">
                Log in
              </Button>
            </Form.Item>
          </Form>
        );
      }
    }
    

      

  • 相关阅读:
    使用Jquery EasyUi常见问题解决方案
    短信平台接口调用方法参考
    linux查找日志技巧
    Linux 上传 启动 删除...命令总结
    java 验证手机号码、电话号码(包括最新的电信、联通和移动号码)
    Web Services 中XML、SOAP和WSDL的一些必要知识
    Mac环境下配置PhpStorm
    Python爬虫刷回复
    Django和layim实现websocket
    Python爬虫刷回复
  • 原文地址:https://www.cnblogs.com/yetiezhu/p/12731454.html
Copyright © 2011-2022 走看看