Introduction:

1. useState:

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

2. useEffect:

Example:

import React, { useState, useEffect } from 'react';
   function Timer() {
     const [seconds, setSeconds] = useState(0);
     useEffect(() => {
       const intervalId = setInterval(() => {
         setSeconds(seconds + 1);
       }, 1000);
       return () => clearInterval(intervalId);
     }, [seconds]);
     return <p>Seconds: {seconds}</p>;
   }
import React, { useContext } from 'react';
   const ThemeContext = React.createContext('light');
   function ThemedButton() {
     const theme = useContext(ThemeContext);
     return <button style={{ background: theme }}>Themed Button</button>;
   }
import React, { 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:
         throw new Error();
     }
   }
   function Counter() {
     const [state, dispatch] = useReducer(reducer, initialState);
     return (
       <div>
         Count: {state.count}
         <button onClick={() => dispatch({ type: 'increment' })}>+</button>
         <button onClick={() => dispatch({ type: 'decrement' })}>-</button>
       </div>
     );
   }
import React, { useState, useCallback } from 'react';
   function MyComponent() {
     const [count, setCount] = useState(0);
     const increment = useCallback(() => setCount(count + 1), [count]);
     return (
       <div>
         <p>Count: {count}</p>
         <button onClick={increment}>Increment</button>
       </div>
     );
   }
import React, { useMemo } from 'react';
function ExpensiveCalculation({ count }) {
const result = useMemo(() => {
return count * 2;
}, [count]);
return <p>Result: {result}</p>;
}
import React, { useRef } from 'react';
function TextInputWithFocusButton() {
const inputRef = useRef(null);
const focusInput = () => {
inputRef.current.focus();
};
return (
<div>
<input ref={inputRef} type="text" />
<button onClick={focusInput}>Focus Input</button>
</div>
);
}
import React, { useRef, useImperativeHandle, forwardRef } from 'react';
   const FancyInput = forwardRef((props, ref) => {
     const inputRef = useRef();
     useImperativeHandle(ref, () => ({
       focus: () => {
         inputRef.current.focus();
       }
     }));
     return <input ref={inputRef} />;
   });
   function ParentComponent() {
     const fancyInputRef = useRef();
     const focusInput = () => {
       fancyInputRef.current.focus();
     };
     return (
       <div>
         <FancyInput ref={fancyInputRef} />
         <button onClick={focusInput}>Focus Input</button>
       </div>
     );
   }
import React, { useState, useDebugValue } from 'react';
    function useCustomHook() {
      const [count, setCount] = useState(0);
      useDebugValue(count > 5 ? 'Count is greater than 5' : 'Count is less than or equal to 5');
      return [count, setCount];
    }

Contact US

Get in Touch

we provide best services. Need Help?