Powerful ReactJS Hooks 1727339763
Powerful ReactJS Hooks 1727339763
REACTJS
HOOKS
HOOKS
Unlock the power of React Hooks
to write expressive, concise, and
high-performance components.
V. useDeferredValue Hook 7
X. useState Hook
How It Works 12
INTRODUCTION
In the fast-evolving world of web development,
React has become one of the most widely-used
libraries for building modern user interfaces.
Since its introduction, React has provided
developers with tools to create dynamic,
responsive applications, but one key innovation
has revolutionized how we write React
components: React Hooks.
function App() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<Button onClick={handleClick} />
</div>
);
}
function UserInfo() {
const user = useContext(UserContext); // Access user from context
return <div>Welcome, {user.name}!</div>;
}
function App() {
const user = { name: 'Sam D' };
return (
<UserContext.Provider value={user}>
<UserInfo />
</UserContext.Provider>
);
}
function useUserStatus(userId) {
const [isOnline, setIsOnline] = useState(false);
return isOnline;
}
function SearchApp() {
const [query, setQuery] = useState('');
const deferredQuery = useDeferredValue(query); // Delays updating the
search query
return (
<div>
<input value={query} onChange={e => setQuery(e.target.value)} />
<ul>{results.map(r => <li key={r}>{r}</li>)}</ul>
</div>
);
}
function Timer() {
const [time, setTime] = useState(0);
useEffect(() => {
const interval = setInterval(() => setTime(time => time + 1), 1000);
function Form() {
const id = useId(); // Generates a unique ID
return (
<div>
<label htmlFor={id}>Username</label>
<input id={id} type="text" />
</div>
);
}
useImperativeHandle(ref, () => ({
focusInput: () => inputRef.current.focus()
}));
function Parent() {
const inputRef = useRef();
return (
<div>
<CustomInput ref={inputRef} />
<button onClick={() =>
inputRef.current.focusInput()}>Focus Input</button>
</div>
);
}
function TextInput() {
const inputRef = useRef(); // Create a ref
return (
<div>
<input ref={inputRef} type="text" />
<button onClick={focusInput}>Focus the Input</button>
</div>
);
}
function Counter() {
const [count, setCount] = useState(0); // useState initializes 'count' to 0
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Increment</button>
</div>
);
}
function LayoutEffectExample() {
const divRef = useRef();
useLayoutEffect(() => {
console.log('LayoutEffect - Div size:',
divRef.current.offsetHeight);
});
function App() {
const [num, setNum] = useState(1);
return (
<div>
<ExpensiveCalculation number={num} />
<button onClick={() => setNum(num + 1)}>Increment
Number</button>
</div>
);
}
function SearchApp() {
const [searchTerm, setSearchTerm] = useState('');
const [results, setResults] = useState([]);
const [isPending, startTransition] = useTransition();
return (
<div>
<input value={searchTerm} onChange={handleSearch} />
{isPending ? <p>Loading...</p> : <ul>{results.map(r =>
<li key={r}>{r}</li>)}</ul>}
</div>
);
}
function useStoreValue() {
return useSyncExternalStore(store.subscribe,
store.getValue);
}
function Counter() {
const value = useStoreValue();
return (
<div>
<p>{value}</p>
<button onClick={() => store.setValue(value +
1)}>Increment</button>
</div>
);
}
function Counter() {
const [state, dispatch] = useReducer(reducer, { count: 0 });
return (
<div>
<p>{state.count}</p>
<button onClick={() => dispatch({ type: 'increment'
})}>Increment</button>
<button onClick={() => dispatch({ type: 'decrement'
})}>Decrement</button>
</div>
);
}
function useWindowWidth() {
const [width, setWidth] = useState(window.innerWidth);
useEffect(() => {
const handleResize = () => setWidth(window.innerWidth);
window.addEventListener('resize', handleResize);
return width;
}
function WindowWidthDisplay() {
const width = useWindowWidth();
CONNECT
MINAL
SONTAKKE
ON LINKEDIN
+123-456-7890
www.reallygreatsite.com