Category: Examples

https://cdn-icons-png.flaticon.com/512/5307/5307812.png

  • Basic Calculator

    A simple calculator for addition, subtraction, multiplication, and division.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function Calculator() {
      const [num1, setNum1] = useState('');
      const [num2, setNum2] = useState('');
      const [result, setResult] = useState(null);
    
      const handleCalculation = (operation) => {
    
    const number1 = parseFloat(num1);
    const number2 = parseFloat(num2);
    let res;
    switch (operation) {
      case 'add':
        res = number1 + number2;
        break;
      case 'subtract':
        res = number1 - number2;
        break;
      case 'multiply':
        res = number1 * number2;
        break;
      case 'divide':
        res = number2 !== 0 ? number1 / number2 : 'Error';
        break;
      default:
        break;
    }
    setResult(res);
    }; return (
    <div>
      <h1>Calculator</h1>
      &lt;input type="number" value={num1} onChange={(e) =&gt; setNum1(e.target.value</code></code></pre>
  • Search Filter

    A simple search filter for a list of items.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    const items = ['Apple', 'Banana', 'Cherry', 'Date', 'Fig', 'Grape'];
    
    function SearchFilter() {
      const [query, setQuery] = useState('');
    
      const filteredItems = items.filter(item =>
    
    item.toLowerCase().includes(query.toLowerCase())
    ); return (
    &lt;div&gt;
      &lt;h1&gt;Search Filter&lt;/h1&gt;
      &lt;input
        type="text"
        value={query}
        onChange={(e) =&gt; setQuery(e.target.value)}
        placeholder="Search..."
      /&gt;
      &lt;ul&gt;
        {filteredItems.map((item, index) =&gt; (
          &lt;li key={index}&gt;{item}&lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<SearchFilter />, document.getElementById('root'));
  • Password Strength Checker

    A simple password strength checker.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function PasswordStrengthChecker() {
      const [password, setPassword] = useState('');
    
      const getStrength = () => {
    
    if (password.length &lt; 6) return 'Weak';
    if (password.length &lt; 12) return 'Moderate';
    return 'Strong';
    }; return (
    &lt;div&gt;
      &lt;h1&gt;Password Strength Checker&lt;/h1&gt;
      &lt;input
        type="password"
        value={password}
        onChange={(e) =&gt; setPassword(e.target.value)}
        placeholder="Enter your password"
      /&gt;
      &lt;p&gt;Password strength: {getStrength()}&lt;/p&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<PasswordStrengthChecker />, document.getElementById('root'));
  • Contact List

    This example displays a simple contact list.

    jsxCopy codeimport React from 'react';
    import ReactDOM from 'react-dom';
    
    const contacts = [
      { name: 'Alice', phone: '123-456-7890' },
      { name: 'Bob', phone: '234-567-8901' },
      { name: 'Charlie', phone: '345-678-9012' },
    ];
    
    function ContactList() {
      return (
    
    &lt;div&gt;
      &lt;h1&gt;Contact List&lt;/h1&gt;
      &lt;ul&gt;
        {contacts.map((contact, index) =&gt; (
          &lt;li key={index}&gt;
            {contact.name} - {contact.phone}
          &lt;/li&gt;
        ))}
      &lt;/ul&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<ContactList />, document.getElementById('root'));
  • Weather App (Static Example)

    This example simulates a weather app interface.

    jsxCopy codeimport React from 'react';
    import ReactDOM from 'react-dom';
    
    function WeatherApp() {
      const weatherData = {
    
    location: 'New York',
    temperature: '22°C',
    condition: 'Sunny',
    }; return (
    &lt;div&gt;
      &lt;h1&gt;Weather App&lt;/h1&gt;
      &lt;h2&gt;{weatherData.location}&lt;/h2&gt;
      &lt;p&gt;{weatherData.temperature}&lt;/p&gt;
      &lt;p&gt;{weatherData.condition}&lt;/p&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<WeatherApp />, document.getElementById('root'));
  • Responsive Grid Layout

    This example creates a simple responsive grid layout using CSS grid.

    jsxCopy codeimport React from 'react';
    import ReactDOM from 'react-dom';
    
    function GridLayout() {
      return (
    
    &lt;div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(100px, 1fr))', gap: '10px' }}&gt;
      {Array.from({ length: 20 }, (_, index) =&gt; (
        &lt;div key={index} style={{ background: '#ccc', padding: '20px', textAlign: 'center' }}&gt;
          Item {index + 1}
        &lt;/div&gt;
      ))}
    &lt;/div&gt;
    ); } ReactDOM.render(<GridLayout />, document.getElementById('root'));
  • Countdown Timer

    A simple countdown timer example.

    jsxCopy codeimport React, { useState, useEffect } from 'react';
    import ReactDOM from 'react-dom';
    
    function CountdownTimer() {
      const [time, setTime] = useState(60);
    
      useEffect(() => {
    
    if (time &gt; 0) {
      const timer = setInterval(() =&gt; setTime(time - 1), 1000);
      return () =&gt; clearInterval(timer);
    }
    }, [time]); return (
    &lt;div&gt;
      &lt;h1&gt;Countdown Timer&lt;/h1&gt;
      &lt;h2&gt;{time} seconds remaining&lt;/h2&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<CountdownTimer />, document.getElementById('root'));
  • Image Gallery

    This example shows a simple image gallery with a click-to-enlarge feature.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    const images = [
      'https://via.placeholder.com/150',
      'https://via.placeholder.com/200',
      'https://via.placeholder.com/250',
    ];
    
    function ImageGallery() {
      const [selectedImage, setSelectedImage] = useState(null);
    
      return (
    
    &lt;div&gt;
      &lt;h1&gt;Image Gallery&lt;/h1&gt;
      &lt;div style={{ display: 'flex', gap: '10px' }}&gt;
        {images.map((image, index) =&gt; (
          &lt;img
            key={index}
            src={image}
            alt={Gallery ${index}}
            style={{ cursor: 'pointer' }}
            onClick={() =&gt; setSelectedImage(image)}
          /&gt;
        ))}
      &lt;/div&gt;
      {selectedImage &amp;&amp; (
        &lt;div onClick={() =&gt; setSelectedImage(null)} style={{ cursor: 'pointer', marginTop: '20px' }}&gt;
          &lt;img src={selectedImage} alt="Selected" style={{ width: '300px', height: 'auto' }} /&gt;
        &lt;/div&gt;
      )}
    &lt;/div&gt;
    ); } ReactDOM.render(<ImageGallery />, document.getElementById('root'));
  • Star Rating Component

    This example demonstrates a star rating system.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function StarRating() {
      const [rating, setRating] = useState(0);
    
      return (
    
    &lt;div&gt;
      &lt;h1&gt;Star Rating&lt;/h1&gt;
      {&#91;1, 2, 3, 4, 5].map((star) =&gt; (
        &lt;span
          key={star}
          onClick={() =&gt; setRating(star)}
          style={{ cursor: 'pointer', color: star &lt;= rating ? 'gold' : 'gray' }}
        &gt;
          ★
        &lt;/span&gt;
      ))}
      &lt;p&gt;Your rating: {rating}&lt;/p&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<StarRating />, document.getElementById('root'));
  • Accordion Component

    This example shows how to create a simple accordion component.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function Accordion() {
      const [openIndex, setOpenIndex] = useState(null);
      
      const toggleAccordion = (index) => {
    
    setOpenIndex(openIndex === index ? null : index);
    }; const items = [
    { title: 'Section 1', content: 'Content for section 1.' },
    { title: 'Section 2', content: 'Content for section 2.' },
    { title: 'Section 3', content: 'Content for section 3.' },
    ]; return (
    &lt;div&gt;
      &lt;h1&gt;Accordion Example&lt;/h1&gt;
      {items.map((item, index) =&gt; (
        &lt;div key={index}&gt;
          &lt;button onClick={() =&gt; toggleAccordion(index)}&gt;
            {item.title}
          &lt;/button&gt;
          {openIndex === index &amp;&amp; &lt;p&gt;{item.content}&lt;/p&gt;}
        &lt;/div&gt;
      ))}
    &lt;/div&gt;
    ); } ReactDOM.render(<Accordion />, document.getElementById('root'));