How to Use Next UI with React.js: Explore Pre-built Components

When building React.js applications, having a robust UI library can drastically speed up your development process and improve the look and feel of your application. Next UI is one such library that provides a collection of customizable and pre-built components designed to work seamlessly with React.js. In this blog, we will walk you through integrating Next UI into your React project, exploring pre-built components, and customizing them to fit your needs.

➡️ Setting Up Your React Project

Before diving into Next UI, you need to set up a React project. Follow these steps:

1. Create a React App

If you don’t already have a React project, you can create one using create-react-app:

npx create-react-app next-ui-demo
cd next-ui-demo

Alternatively, if you’re using Vite for a faster setup:

npm create vite@latest next-ui-demo --template react
cd next-ui-demo
npm install

2. Install Next UI

Next UI is not part of the default React framework, so you’ll need to install it:

npm install @nextui-org/react

This command installs the Next UI library and its dependencies.

3. Set Up Next UI in Your Project

Next UI requires you to wrap your application in its NextUIProvider. Open your src/index.js (or src/main.jsx if using Vite) and wrap your app like this:

import React from 'react';
import ReactDOM from 'react-dom/client';
import { NextUIProvider } from '@nextui-org/react';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <NextUIProvider>
       <App />
    </NextUIProvider>
  </React.StrictMode>
);

You’re now ready to start using Next UI components in your React project!

➡️ Exploring Pre-built Components

Next UI comes with a wide range of pre-built components to speed up development. Let’s explore some of the most commonly used components.

1. Button

The Button component is one of the most fundamental components in any UI library. Here’s how you can use it:

import { Button } from '@nextui-org/react';

const App = () => {
  return (
    <div>
       <Button>Default Button</Button>
       <Button color="primary">Primary Button</Button>
       <Button color="secondary">Secondary Button</Button>
    </div>
  );
};

export default App;

Features of the Button Component:

▪️Variants: Buttons can be styled as outlined, ghost, or flat.
▪️Colors: Supports predefined colors like primary, secondary, success, warning, and error.

2. Card

The Card component is perfect for displaying grouped content, such as images and text.

import { Card, Text } from '@nextui-org/react';

const App = () => {
  return (
   <Card css={{ mw: "400px" }}>
     <Card.Header>
       <Text b>Card Title</Text>
     </Card.Header>
     <Card.Body>
       <Text>This is the card body content.</Text>
     </Card.Body>
     <Card.Footer>
       <Text small>Card Footer</Text>
     </Card.Footer>
  </Card>
 );
};

export default App;

Features of the Card Component:

▪️Customizable headers, bodies, and footers.
▪️Supports images, icons, and actions like buttons.

3. Input

The Input component lets you quickly create text fields and forms.

import { Input } from '@nextui-org/react';

const App = () => {
  return (
    <div>
      <Input label="Username" placeholder="Enter your username" />
      <Input.Password label="Password" placeholder="Enter your password" />
    </div>
  );
};

export default App;

Features of the Input Component:

▪️Built-in validation styles.
▪️Variants for password fields, search inputs, etc.

4. Modal

The Modal component is used for creating popups or overlays.

import { Modal, Button, Text } from '@nextui-org/react';
import { useState } from 'react';

const App = () => {
  const [visible, setVisible] = useState(false);
  const openModal = () => setVisible(true);
  const closeModal = () => setVisible(false);

  return (
    <div>
      <Button onClick={openModal}>Open Modal</Button>
      <Modal open={visible} onClose={closeModal}>
        <Modal.Header>
          <Text b>Modal Title</Text>
        </Modal.Header>
        <Modal.Body>
          <Text>This is the modal content.</Text>
        </Modal.Body>
        <Modal.Footer>
          <Button auto onClick={closeModal}>
            Close
          </Button>
        </Modal.Footer>
      </Modal>
    </div>
  );
};

export default App;

Features of the Modal Component:

▪️Fully responsive and customizable.
▪️Supports multiple modals stacked together.

Try Next UI and Speed Up Your React Development Now

➡️ Customizing Components

One of the best things about Next UI is its customization capabilities. You can use the css prop to customize components or override default styles.

Example: Customizing a Button

import { Button } from '@nextui-org/react';

const App = () => {
  return (
    <div>
      <Button css={{ backgroundColor: '#ff6347', color: '#fff' }}>
        Custom Button
      </Button>
    </div>
  );
};

export default App;

Explanation:

▪️The css prop allows you to use CSS-in-JS syntax to apply custom styles.
▪️You can modify colors, padding, margins, and other properties.

Example: Customizing a Card

import { Card, Text } from '@nextui-org/react';

const App = () => {
   return (
     <Card css={{ mw: "300px", backgroundColor: '#f0f0f0', padding: '20px' }}>
       <Card.Header>
          <Text b css={{ color: '#ff6347' }}>Custom Card Title</Text>
       </Card.Header>
       <Card.Body>
          <Text>This card has custom styles.</Text>
       </Card.Body>
     </Card>
   );
};
export default App;

➡️ Best Practices

To ensure a smooth experience when using Next UI, follow these best practices:

1. Use the CSS Prop for Customization: Avoid modifying the library’s core styles directly. Use the CSS prop to override styles per component.
2. Leverage Pre-built Themes: Next UI supports themes to create a consistent look across your app.
3. Optimize for Accessibility: Ensure components are accessible by providing proper labels, roles, and ARIA attributes.
4. Minimize Custom Styles: Stick to the library’s design system as much as possible to maintain visual consistency.
5. Keep Components Modular: Avoid bloating your components with too many customizations. Break them into smaller pieces if needed.

coma

Conclusion

Next UI is a powerful library that provides a rich set of pre-built components to accelerate your development process. Whether you’re creating buttons, cards, modals, or inputs, Next UI has you covered. With its strong focus on customization and accessibility, you can build beautiful, user-friendly interfaces with minimal effort.

By following the steps and best practices outlined in this blog, you’re well-equipped to integrate Next UI into your React projects and start exploring its full potential.

Keep Reading

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?