burme-coder-max / data /knowledge /skills /react_skills.md
amkyawdev's picture
Upload folder using huggingface_hub
a7d7463 verified
|
Raw
History Blame Contribute Delete
3.21 kB

React Skills

Hooks

useState

import { useState } from 'react';

function Counter() {
    const [count, setCount] = useState(0);
    
    return (
        <div>
            <p>Count: {count}</p>
            <button onClick={() => setCount(count + 1)}>
                Increment
            </button>
        </div>
    );
}

useEffect

import { useState, useEffect } from 'react';

function DataFetcher({ url }) {
    const [data, setData] = useState(null);
    const [loading, setLoading] = useState(true);
    
    useEffect(() => {
        fetch(url)
            .then(res => res.json())
            .then(data => {
                setData(data);
                setLoading(false);
            });
    }, [url]);
    
    return loading ? <p>Loading...</p> : <div>{data}</div>;
}

useContext

import { createContext, useContext } from 'react';

const ThemeContext = createContext('light');

function App() {
    return (
        <ThemeContext.Provider value="dark">
            <Child />
        </ThemeContext.Provider>
    );
}

function Child() {
    const theme = useContext(ThemeContext);
    return <div className={theme}>Theme: {theme}</div>;
}

useReducer

import { useReducer } from 'react';

const initialState = { count: 0 };

function reducer(state, action) {
    switch (action.type) {
        case 'increment':
            return { count: state.count + 1 };
        case 'decrement':
            return { count: state.count - 1 };
        default:
            return state;
    }
}

function Counter() {
    const [state, dispatch] = useReducer(reducer, initialState);
    
    return (
        <div>
            <p>{state.count}</p>
            <button onClick={() => dispatch({ type: 'increment' })}>
                +
            </button>
        </div>
    );
}

Custom Hooks

function useLocalStorage(key, initialValue) {
    const [storedValue, setStoredValue] = useState(() => {
        try {
            const item = window.localStorage.getItem(key);
            return item ? JSON.parse(item) : initialValue;
        } catch (error) {
            return initialValue;
        }
    });

    const setValue = (value) => {
        setStoredValue(value);
        window.localStorage.setItem(key, JSON.stringify(value));
    };

    return [storedValue, setValue];
}

Components

Functional Component

function Welcome({ name, age }) {
    return (
        <div className="welcome">
            <h1>Hello, {name}!</h1>
            <p>Age: {age}</p>
        </div>
    );
}

Props

// Parent
<Welcome name="John" age={30} />

// Child - receieves props
function Welcome(props) {
    return <h1>Hello {props.name}</h1>;
}

Forms

Controlled Input

function Form() {
    const [name, setName] = useState('');
    
    const handleSubmit = (e) => {
        e.preventDefault();
        console.log(name);
    };
    
    return (
        <form onSubmit={handleSubmit}>
            <input 
                value={name}
                onChange={(e) => setName(e.target.value)}
            />
            <button type="submit">Submit</button>
        </form>
    );
}