Logo

dev-resources.site

for different kinds of informations.

How to Use Shadcn UI with a React Project

Published at
8/7/2024
Categories
shadcn
tutorial
webdev
react
Author
sanditzz
Categories
4 categories in total
shadcn
open
tutorial
open
webdev
open
react
open
Author
8 person written this
sanditzz
open
How to Use Shadcn UI with a React Project

Shadcn UI is a modern and flexible UI component library designed to work seamlessly with React. It provides a set of reusable components that can help you build beautiful and responsive user interfaces quickly. In this blog post, I'll walk you through the steps to integrate Shadcn UI into your React project.


Prerequisites

Before we begin, make sure you have the following installed on your machine:

  • Node.js (v12 or higher)
  • npm or yarn
  • A React project (created using Create React App, Vite, Next.js, or any other setup)

Step 1: Install Shadcn UI

First, you need to install Shadcn UI and its peer dependencies. You can do this using npm or yarn. Open your terminal and navigate to your React project directory, then run the following command:

# Using npm
npm install @shadcn/ui

# Using yarn
yarn add @shadcn/ui
Enter fullscreen mode Exit fullscreen mode

Step 2: Using Shadcn UI Components

Shadcn UI provides a CLI tool to generate components easily. Let's use npx shadcn to add some components to our project.

Adding a Button Component

To add a Button component, run the following command:

npx shadcn add button
Enter fullscreen mode Exit fullscreen mode

This command will generate a Button component in your project. You can now use it in your React components. Here's an example:

// src/App.js
import React from 'react';
import { Button } from '@shadcn/ui';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <h1>Welcome to Shadcn UI</h1>
        <Button onClick={() => alert('Button clicked!')}>Click Me</Button>
      </header>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Adding a Card Component

To add a Card component, run the following command:

npx shadcn add card
Enter fullscreen mode Exit fullscreen mode

This will generate a Card component. You can use it as follows:

// src/App.js
import React from 'react';
import { Button, Card } from '@shadcn/ui';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <h1>Welcome to Shadcn UI</h1>
        <Card>
          <h2>Card Title</h2>
          <p>This is a card component.</p>
          <Button onClick={() => alert('Button inside card clicked!')}>Click Me</Button>
        </Card>
      </header>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Step 3: Customizing Components

Shadcn UI components are highly customizable. You can pass props to customize their appearance and behavior. For example, you can customize the Button component like this:

// src/App.js
import React from 'react';
import { Button, Card } from '@shadcn/ui';

function App() {
  return (
    <div className="App">
      <header className="App-header">
        <h1>Welcome to Shadcn UI</h1>
        <Card>
          <h2>Card Title</h2>
          <p>This is a card component.</p>
          <Button color="primary" size="large" onClick={() => alert('Button inside card clicked!')}>
            Click Me
          </Button>
        </Card>
      </header>
    </div>
  );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this guide, we've seen how to set up Shadcn UI in a React project and how to use its components. Shadcn UI makes it easy to build beautiful and responsive user interfaces with minimal effort. Explore the Shadcn UI documentation for more components and customization options.

Tips for Using Shadcn UI

  1. Explore the Documentation: Always refer to the official Shadcn UI documentation for the most up-to-date information on components, customization options, and usage examples.

  2. Component Customization: Shadcn UI components are designed to be highly customizable. Make use of props to adjust the appearance and behavior of components to fit your needs.

  3. Theming: Utilize the theming capabilities of Shadcn UI to maintain a consistent look and feel across your application. Check the documentation for details on how to implement and customize themes.

  4. CLI Tool: Use the npx shadcn CLI tool to quickly add components to your project. This can save you time and ensure that you are using the components correctly.

Thanks for reading!
Po.

shadcn Article's
30 articles in total
Favicon
Made FOSS for simplifying NextJS dev with OAuth And Postgres
Favicon
.gitkeep file in Shadcn/ui source code
Favicon
Introducing the Zod Schema Designer: A Visual Tool for Creating and Editing Zod Schemas
Favicon
Handling Local Storage for Toaster Notifications
Favicon
I created a visually pleasing HTML color viewer
Favicon
AISEKA Color Tool Website
Favicon
Color Palette is served with ShadCn Theme Editor
Favicon
Create React project with Vite and set up Tailwind, shadcn/ui
Favicon
Why Developers Love the Shadcn Sidebar Component
Favicon
A comparison of metadata configurations between Lobechat and Shadcn/ui
Favicon
Forms in seconds with ShardCn forms
Favicon
What is the Difference Between Radix UI and ShadCN?
Favicon
How Shadcn CLI uses error constants to improve code readability
Favicon
Exploring the ShadCN UI Library: A Comprehensive Guide
Favicon
10 Essential Shadcn Components Every Developer Should Know About
Favicon
The beginning of my open source journey
Favicon
Install ShadCN and Tailwind in Vite React Project
Favicon
How to add shadcn to existing project
Favicon
How to Use Shadcn UI with a React Project
Favicon
How to Install Shadcn with Next.js 14: A Step-by-Step Guide
Favicon
Shadcn Date Picker with custom implementation
Favicon
How to Fix Shadcn UI Adding Wrong Folder for Components
Favicon
Material UI vs Shadcn
Favicon
Building UIs with Franken UI, a Shadcn alternative
Favicon
Implement ShadCn form with Validation
Favicon
Top 5 Coolest shadcn/ui Extensions
Favicon
Shadcn UI: Must-Have Tools & Resources
Favicon
shadcn-ui/ui codebase analysis: examples route explained.
Favicon
Two ways I discovered to pass dynamic parameters in (arrow and non-arrow) function.
Favicon
Creating a User Nav bar in Next.js 14 using Shadcn UI and Tailwindย CSS

Featured ones: