Skip to content

Instantly share code, notes, and snippets.

@santospatrick
Last active April 18, 2021 19:06
Show Gist options
  • Save santospatrick/073b9d8fea9873b7a373439d9bdc7fe6 to your computer and use it in GitHub Desktop.
Save santospatrick/073b9d8fea9873b7a373439d9bdc7fe6 to your computer and use it in GitHub Desktop.
Right and Wrong way to use Ant Design with React.js

References:

A Simple Signin Form

Signin Form Signin Form as components
signin signin

1. Usage of <Button />

Bad:

import styled from 'styled-components';

const LoginButton = styled.button`
  background-color: #FF377B;
  width: 100%;
  font-family: Poppins;
`

function SigninForm() {
  return (
    <form>
      // ...
      <LoginButton />
    </form>
  )
}

Why is this a bad ❌ example?

  1. Recreating an existing component Ant Design already implements.
  2. Setting width: 100% in styles, but Ant Design's button already has a prop for that.
  3. Setting font-family locally, instead of globally

Good:

// SigninForm.js
import { Button } from 'antd';

function SigninForm() {
  return (
    <form>
      // ...
      <Button block>Sign In</Button>
    </form>
  )
}

// theme.js
module.exports = {
  "@font-family": "Poppins",
  "@primary-color": "#FF377B"
}

Why is this a good 🥰 example?

  1. Using Ant Design's <Button /> component
  2. Using <Button /> prop block to fulfill needed width
  3. Customizing all components primary color (#FF377B), not only <Button /> local styles
  4. Setting variables in theme.js makes them reusable in all components, even third parties that sometimes we need.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment