1. Home
  2. /
  3. Chakra-ui
  4. /
  5. Chakra UI Counter Example

Chakra UI Counter Example - Chakra-ui Typing CST Test

Loading…

Chakra UI Counter Example — Chakra-ui Code

Demonstrates a simple counter layout using Chakra UI components and React for interactivity.

import React, { useState } from 'react';
import { ChakraProvider, Box, Button, Heading, extendTheme } from '@chakra-ui/react';

const theme = extendTheme({ config: { initialColorMode: 'light', useSystemColorMode: false } });

const Counter = () => {
	const [count, setCount] = useState(0);
	const [isDark, setIsDark] = useState(false);

	return (
		<ChakraProvider theme={theme}>
		<Box textAlign='center' mt={10} bg={isDark ? 'gray.800' : 'gray.100'} color={isDark ? 'white' : 'black'} p={6} borderRadius='md'>
		<Heading mb={4}>Counter: {count}</Heading>
		<Button colorScheme='blue' mr={2} onClick={() => setCount(count + 1)}>+</Button>
		<Button colorScheme='red' mr={2} onClick={() => setCount(count - 1)}>-</Button>
		<Button colorScheme='gray' onClick={() => setCount(0)}>Reset</Button>
		<br /><br />
		<Button colorScheme='yellow' onClick={() => setIsDark(!isDark)}>Switch Theme</Button>
		</Box>
		</ChakraProvider>
	);
};

export default Counter;

Chakra-ui Language Guide

Chakra UI is a modular and accessible React component library that provides a set of composable, themeable, and responsive UI components.

Primary Use Cases

  • ▸React-based web applications
  • ▸Dashboards and admin panels
  • ▸Rapid prototyping with composable components
  • ▸Responsive and accessible UI projects
  • ▸Customizable component libraries with theming

Notable Features

  • ▸Accessible components with built-in ARIA support
  • ▸Style props for inline styling
  • ▸Themeable design system with global overrides
  • ▸Composable components (Box, Stack, Flex)
  • ▸Responsive props and utility-first styling

Origin & Creator

Created by Segun Adebayo in 2019 as a modern, developer-friendly React UI library.

Industrial Note

Chakra UI is popular for React projects that need quick prototyping, accessible components, and highly customizable UI without enforcing a specific design system.

More Chakra-ui Typing Exercises

Chakra UI Button ExampleChakra UI Card ExampleChakra UI Modal ExampleChakra UI Tabs ExampleChakra UI Form ExampleChakra UI Alert ExampleChakra UI Progress ExampleChakra UI Accordion ExampleChakra UI Tooltip Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher