Semantic UI React
Semantic UI React is the official React integration for the Semantic UI framework, providing a comprehensive collection of UI components designed to deliver consistent and aesthetically pleasing user interfaces. The current stable version is 2.1.5, with active development on a v3.0.0 beta branch, indicating ongoing maintenance and future feature enhancements. The library distinguishes itself through its declarative API, allowing developers to construct complex UIs with concise JSX. It heavily leverages 'shorthand props,' enabling a more compact and readable component tree by accepting primitive values or objects to render sub-components, which is a key differentiator. It also ships with first-class TypeScript support, offering robust type definitions for all its components and props, making it highly compatible with modern typed React workflows. While it requires the Semantic UI CSS toolkit for styling, it provides the React component logic, facilitating rapid development of visually consistent web applications. The release cadence for stable versions is moderate, with more frequent bug fixes and beta updates for the next major version. This package is an excellent choice for projects seeking a complete, opinionated UI toolkit with a strong emphasis on consistent design and developer experience within the React ecosystem.
Common errors
-
Module not found: Can't resolve 'semantic-ui-react'
cause The `semantic-ui-react` package has not been installed or is not correctly linked in your project.fixRun `npm install semantic-ui-react` or `yarn add semantic-ui-react` to add the package to your project dependencies. -
TypeError: Cannot read properties of undefined (reading 'call') at Object.createReactClass.displayName [as render]
cause This often occurs when trying to use a component that is not correctly imported or when `this` context is lost in class components. Could also be a mismatch in React versions or a build configuration issue.fixVerify that all Semantic UI React components are correctly imported as named exports. Ensure your `react` and `react-dom` peer dependencies match the library's requirements. For class components, bind event handlers correctly. -
Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause This error typically indicates that you are trying to render something that React doesn't recognize as a valid component. This could be due to a wrong import (e.g., `import * as SUI from 'semantic-ui-react'` and then trying to use `<SUI.Button>` without explicit named exports, or a default import where a named import is expected).fixEnsure you are using named imports for Semantic UI React components, e.g., `import { Button } from 'semantic-ui-react';`. Avoid default imports unless explicitly specified by the library for a particular component. -
Warning: Failed prop type: The prop `children` is marked as required in `YourComponent`, but its value is `undefined`.
cause Semantic UI React components often have strict prop types. This warning indicates a required prop (like `children` or another specific prop) was not provided or was `undefined`.fixReview the component's documentation for required props. Ensure all mandatory props are passed and have valid values. For example, a `Message` component might expect `header` or `content`.
Warnings
- breaking The `Ref` component was removed in v3.0.0-beta.0. All components now support native React ref forwarding directly. Components that previously relied on `Ref` will need to be updated.
- gotcha Semantic UI React provides only the component logic and markup. It does not include the styling itself. You MUST include the Semantic UI CSS toolkit in your project, typically by importing `semantic-ui-css/semantic.min.css`.
- breaking Type definitions have been updated in v2.1.3 and v2.1.4 to improve compatibility with TypeScript 4.8+ and `@types/react@18`. Older TypeScript versions or `@types/react` might encounter type errors.
- gotcha Semantic UI React components rely on specific CSS classes for styling. If you're using a custom build of Semantic UI CSS or a theming solution, ensure that all required classes are present, otherwise, components may appear unstyled or broken.
- deprecated Earlier versions might have included direct references to `React.PropTypes`. While no longer used, ensure your project's ESLint rules or build processes don't incorrectly flag these as deprecated issues if referencing older code examples.
Install
-
npm install semantic-ui-react -
yarn add semantic-ui-react -
pnpm add semantic-ui-react
Imports
- Button
const Button = require('semantic-ui-react').Button;import { Button } from 'semantic-ui-react'; - Form
import { Form, Input, TextArea, Button } from 'semantic-ui-react'; - SemanticShorthandItem
import type { SemanticShorthandItem } from 'semantic-ui-react';
Quickstart
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Button, Form, Segment, Grid, Message } from 'semantic-ui-react';
import 'semantic-ui-css/semantic.min.css'; // Don't forget to include Semantic UI CSS!
function App() {
const [name, setName] = useState('');
const [email, setEmail] = useState('');
const [submitted, setSubmitted] = useState(false);
const handleSubmit = () => {
setSubmitted(true);
console.log({ name, email });
// In a real app, you would send this data to a server
};
return (
<Grid centered columns={2} style={{ marginTop: '5em' }}>
<Grid.Column>
<Segment>
<Message
attached
header='Welcome to our site!'
content='Fill out the form below to sign-up for a new account'
/>
<Form className='attached fluid segment' onSubmit={handleSubmit}>
<Form.Input
label='Name'
placeholder='First Name'
value={name}
onChange={(e) => setName(e.target.value)}
required
/>
<Form.Input
label='Email'
placeholder='example@domain.com'
type='email'
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
<Button primary type='submit'>Submit</Button>
</Form>
{submitted && (
<Message success>
<Message.Header>Form Submitted</Message.Header>
<p>Name: {name}, Email: {email}</p>
</Message>
)}
</Segment>
</Grid.Column>
</Grid>
);
}
const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
root.render(<App />);