![]() |
VOOZH | about |
In React, built-in Hooks are like tools that empower your components with various functionalities. They give you the flexibility to use different features without having to create class components. You can either utilize the hooks provided by React or mix and match them to create custom hooks tailored to your needs. Here's a list of all the pre-built hooks available in React.
State in React allows a component to keep track of information such as user input or the current state of the component. For instance, imagine a form where you type in your name. State would remember what you typed. Similarly, in an image gallery, state would remember which image you selected to display. It's like a memory bank for your component to hold onto important data.
When using state in a component add one of these state.
const Counter () =>{
cosnt [count, steCount] = useState('0');
//....
}
Context in React acts as a global store for sharing data between components, allowing distant components to access this data without the need for prop drilling, thereby simplifying state management and making it more efficient.
it is used to consume data from a Context in a functional component.const Theme ()=>{
const themeDark = useContext(ThemeContext);
//....
}
React refs store non-rendering data like DOM node references or timeout IDs, without triggering re-renders. They enable interaction with non-React systems, like browser APIs, aiding integration with external libraries and imperative logic. Serving as an "escape hatch" from React's standard data flow, refs offer flexibility and enhance compatibility with diverse environments.
ref with functional components.const App () =>{
const myRef = useRef(null);
const handleClick = () => {
myRef.current.focus();
};
}
Effects in React allow components to interact with and stay synchronized with external systems, such as handling network requests, manipulating the browser's DOM, managing animations, integrating with widgets from other UI libraries, and working with non-React code. Essentially, effects help components communicate and coordinate with the world outside of React.
const ClassRoom ()=>{
useEffect(() => {
// Effect code here
return () => {
//Here clean up code };
}, [dependencies]);
}
There are two different useEffect approach that are rarely used.
To make your React app run faster, a smart strategy is to avoid doing unnecessary tasks. For instance, you can instruct React to reuse previous calculations that are already saved, or to avoid redoing a render if the data hasn't changed since the last time. This helps your app be more efficient and responsive.
Used this to skip calculation and unnecessary re-rendirng, one of these are.
const TodoFucntion ()=>{
const seeTodos = useMemo(() =>
filterTodos(todos, tab), [todos, tab]);
//...
}
Components in React can access resources without storing them as part of their state. For instance, a component can retrieve a message from a Promise or obtain styling information from a context without needing to manage that data internally. This approach simplifies component logic and promotes more efficient resource utilization.
If you read a value from a resource the use this hook.
const ChatComponent({ chatPromise }) => {
const chat = use(chatPromise);
const themeDark = use(ThemeContext);
// ...
}
Example : Below is an example of built-in React Hooks.
Output: