Author: saqibkhan

  • 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 (
    
    <div>
      <h1>Contact List</h1>
      <ul>
        {contacts.map((contact, index) => (
          <li key={index}>
            {contact.name} - {contact.phone}
          </li>
        ))}
      </ul>
    </div>
    ); } 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'));
  • Theme Switcher

    This example demonstrates how to toggle between light and dark themes.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function App() {
      const [darkMode, setDarkMode] = useState(false);
    
      const toggleTheme = () => setDarkMode(!darkMode);
    
      return (
    
    &lt;div style={{ background: darkMode ? '#333' : '#FFF', color: darkMode ? '#FFF' : '#000', padding: '20px' }}&gt;
      &lt;h1&gt;Theme Switcher&lt;/h1&gt;
      &lt;button onClick={toggleTheme}&gt;
        Switch to {darkMode ? 'Light' : 'Dark'} Mode
      &lt;/button&gt;
      &lt;p&gt;This is a simple theme switcher example!&lt;/p&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<App />, document.getElementById('root'));
  • Responsive Navigation Menu

    This example shows a simple responsive navigation menu.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function NavBar() {
      const [isOpen, setIsOpen] = useState(false);
    
      const toggleMenu = () => setIsOpen(!isOpen);
    
      return (
    
    &lt;nav&gt;
      &lt;button onClick={toggleMenu}&gt;
        {isOpen ? 'Close Menu' : 'Open Menu'}
      &lt;/button&gt;
      {isOpen &amp;&amp; (
        &lt;ul&gt;
          &lt;li&gt;Home&lt;/li&gt;
          &lt;li&gt;About&lt;/li&gt;
          &lt;li&gt;Contact&lt;/li&gt;
        &lt;/ul&gt;
      )}
    &lt;/nav&gt;
    ); } ReactDOM.render(<NavBar />, document.getElementById('root'));
  • Modal Component

    A simple modal component implementation.

    jsxCopy codeimport React, { useState } from 'react';
    import ReactDOM from 'react-dom';
    
    function Modal({ isOpen, onClose }) {
      if (!isOpen) return null;
    
      return (
    
    &lt;div style={modalStyle}&gt;
      &lt;h2&gt;Modal Title&lt;/h2&gt;
      &lt;p&gt;This is a simple modal!&lt;/p&gt;
      &lt;button onClick={onClose}&gt;Close&lt;/button&gt;
    &lt;/div&gt;
    ); } const modalStyle = { position: 'fixed', top: '50%', left: '50%', transform: 'translate(-50%, -50%)', padding: '20px', backgroundColor: 'white', boxShadow: '0 4px 8px rgba(0, 0, 0, 0.2)', }; function App() { const [isModalOpen, setModalOpen] = useState(false); return (
    &lt;div&gt;
      &lt;h1&gt;Modal Example&lt;/h1&gt;
      &lt;button onClick={() =&gt; setModalOpen(true)}&gt;Open Modal&lt;/button&gt;
      &lt;Modal isOpen={isModalOpen} onClose={() =&gt; setModalOpen(false)} /&gt;
    &lt;/div&gt;
    ); } ReactDOM.render(<App />, document.getElementById('root'));