Posts

Showing posts with the label Code snippets

How to create custom hook in react?

Image
In React, a custom hook is a JavaScript function that starts with the word “use” and that can call other hooks. Custom hooks allow you to extract component logic into reusable functions. To create a custom hook, you can define a function that starts with the word “use” and that includes any logic that you want to reuse. You can then call this function within your components to access the logic it contains. Here is an example of a simple custom hook that manages the state of a toggle button: This custom hook uses the built-in  useState  hook to manage a piece of state that represents whether the toggle button is on or off. It also defines a function called  toggle  that can be used to toggle the state. The hook returns an array that contains the current state and the toggle function. Then you can use this custom hook in your component: In this example, the  MyToggleButton  component calls the  useToggle  custom hook and destructures the returned ar...

How to Quickly Fetch User Contacts in React Native

Image
How to Quickly Fetch User Contacts in React Native Contacts play a crucial role in the digital world, whether it's making phone calls or finding friends on social media. On a smartphone, the bulky and cumbersome phone number book of yesteryear is now a compact list. Telephone numbers, email addresses, and other contact information are all unique to each individual. We'll look at React Native Contacts, a powerful contacts component that lets you use your React Native app to fetch, update, and add new contacts. We'll also go over a similar Expo module. You should be familiar with the fundamentals of React Native and Expo, including JSX, class and functional components, and styling, in order to follow along. This guide assumes that you have already completed the app's basic setup. In React Native, you can fetch the user’s contacts by using the react-native-contacts library. This library provides an easy-to-use API for accessing the device's contact list. To install the...

How to Optimize React Performance with useMemo

Image
How to Optimize React Performance with useMemo Brief on how useMemo works A memoized value is returned by the useMemo Hook in React. Memoization is similar to saving a value from being recalculated by caching it. The only time the useMemo Hook runs is when one of its dependencies changes. This can help us to improve it's performance  Usememo syntax useMemo(calculateValue, dependencies) How to improve Performance by using  useMemo Using the useMemo Hook, you can stop expensive, resource-intensive functions from running all the time. We have an expensive function that runs on every render in this example. There will be a delay in execution when you change the count or add a to-do. A function that performs poorly. Every render calls the expensiveOperation function: import { useState } from "react"; import ReactDOM from "react-dom/client"; const App = () => {   const [count, setCount] = useState(0);   const [todos, setTodos] = useState([]);   const calculat...