<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rohit Singh Rajput</title>
    <description>The latest articles on DEV Community by Rohit Singh Rajput (@rohit413).</description>
    <link>https://dev.to/rohit413</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1839368%2F23466e42-0ca2-4371-9762-b9c05c9c4968.jpg</url>
      <title>DEV Community: Rohit Singh Rajput</title>
      <link>https://dev.to/rohit413</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohit413"/>
    <language>en</language>
    <item>
      <title>20 tricky JavaScript string-related program output questions</title>
      <dc:creator>Rohit Singh Rajput</dc:creator>
      <pubDate>Wed, 04 Sep 2024 06:12:07 +0000</pubDate>
      <link>https://dev.to/rohit413/20-tricky-javascript-string-related-program-output-questions-228k</link>
      <guid>https://dev.to/rohit413/20-tricky-javascript-string-related-program-output-questions-228k</guid>
      <description>&lt;p&gt;Below is a compilation of 20 challenging JavaScript string-related programming questions, each accompanied by its solution and a brief explanation. These questions delve into various string manipulations, covering topics like concatenation, character extraction, case transformations, and more. Ideal for testing and enhancing your understanding of JavaScript’s string handling capabilities, these questions provide a comprehensive overview of common scenarios and nuances in string operations, offering valuable insights for developers looking to deepen their JavaScript expertise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 1:

console.log('hello' - 'world');

Answer:

NaN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;Subtracting strings is not a valid operation in JavaScript, resulting in NaN (Not a Number).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 2:

console.log('hello' + 'world');

Answer :

helloworld
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;Using the + operator concatenates strings, producing the combined string 'helloworld'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 3:

console.log('hello'.length);

Answer :

5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The length property returns the number of characters in the string, which is 5 for 'hello'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 4:

console.log('hello'.charAt(0));

Answer :

h
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The charAt(0) method returns the character at the specified index, here 'h' at index 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 5:

console.log('hello'.toUpperCase());

Answer :

HELLO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;toUpperCase() converts all characters in the string to uppercase, resulting in 'HELLO'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 6:

console.log('hello' === 'Hello');

Answer :

false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;Strict equality (===) compares both value and case, so 'hello' is not equal to 'Hello'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 7:

console.log('hello'.split(''));

Answer :

['h', 'e', 'l', 'l', 'o']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;split('') splits the string into an array of characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 8:

console.log('hello'.replace('l', 'L'));

Answer :

heLlo
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;replace('l', 'L') replaces the first occurrence of 'l' with 'L'.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 9:

console.log('hello'.indexOf('l'));

Answer :

2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;indexOf('l') returns the index of the first occurrence of 'l' in the string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 10:

console.log('hello'[1]);

Answer :

e
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;Accessing the character at index 1 using square brackets results in ‘e’.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 11:

console.log('hello'.concat(' ', 'world'));

Answer :

hello world
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The concat method appends 'world' to 'hello' with a space in between.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 12:

console.log('hello' + 5);

Answer :

hello5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The + operator concatenates the string 'hello' with the number 5.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 13:

console.log('hello' - 5);

Answer :

NaN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;Subtracting a number from a string results in NaN.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 14:

console.log('hello'.repeat(3));

Answer :

hellohellohello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The repeat method duplicates 'hello' three times.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 15:

console.log('hello'.endsWith('o'));

Answer :

true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;Checks if ‘hello’ ends with the character ‘o’, resulting in true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 16:

console.log('hello'.includes('ll'));

Answer :

true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The includes method checks if the string contains the specified substring ('ll' in this case).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 17:

console.log('hello'.slice(1, 4));

Answer :

ell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The slice method extracts a portion of the string (from index 1 to 3).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 18:

console.log('hello'.padStart(8, '_'));

Answer :

___hello
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The padStart method pads the string with underscores to reach a length of 8 characters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 19:

console.log('hello'.charAt(10));

Answer :

''
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;Accessing a character beyond the string’s length returns an empty string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Question 20:

console.log('hello'.substring(2, 4));

Answer :

ll
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Explanation :&lt;/p&gt;

&lt;p&gt;The substring method extracts characters from index 2 to 3 (excluding the end index).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In conclusion, the set of 20 JavaScript string-related programming questions serves as a robust resource for developers aiming to refine their skills. Covering diverse aspects of string manipulation, the questions explore concatenation, character extraction, case transformations, and other fundamental operations. The accompanying answers and explanations offer a comprehensive understanding of intricate scenarios, fostering a deeper insight into JavaScript’s string handling capabilities. These questions not only facilitate proficiency in string-related concepts but also encourage critical thinking and problem-solving skills. Whether preparing for interviews or seeking to bolster one’s JavaScript expertise, this collection provides a valuable opportunity for developers to master the nuances of string manipulation in the JavaScript programming language.&lt;/strong&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>All React hooks in one short.</title>
      <dc:creator>Rohit Singh Rajput</dc:creator>
      <pubDate>Sat, 24 Aug 2024 12:20:05 +0000</pubDate>
      <link>https://dev.to/rohit413/all-react-hooks-in-one-short-2k4n</link>
      <guid>https://dev.to/rohit413/all-react-hooks-in-one-short-2k4n</guid>
      <description>&lt;p&gt;React hooks are a powerful feature in React that allow you to add state and other features to functional components. They were introduced in React 16.8 and have since become an integral part of React development. In this blog, we’ll explore some of the most commonly used React hooks and provide easy-to-understand examples.&lt;/p&gt;

&lt;p&gt;Lets take look at the various hooks :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic Hooks&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useState&lt;/li&gt;
&lt;li&gt;useEffect&lt;/li&gt;
&lt;li&gt;useContext&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Additional Hooks&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useReducer&lt;/li&gt;
&lt;li&gt;useMemo&lt;/li&gt;
&lt;li&gt;useCallback&lt;/li&gt;
&lt;li&gt;useRef&lt;/li&gt;
&lt;li&gt;useLayoutEffect&lt;/li&gt;
&lt;li&gt;useImperativeHandle&lt;/li&gt;
&lt;li&gt;useDebugValue&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;1: useState hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useState hook allows you to add state to a functional component. It takes an initial value as an argument and returns an array with two elements: the current state value and a function to update it.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useState to add a counter to a functional component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const increment= () =&amp;gt; {
    setCount(count + 1);
  }
const decrement = () =&amp;gt; {
    setCount(count - 1);
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;Count: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={increment}&amp;gt;Increment&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={decrement}&amp;gt;decrement&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we start with a count of 0 and update it every time the “Increment” and ‘’decrement’’ button is clicked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2: useEffect hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useEffect hook allows you to perform side effects in a functional component. Side effects include things like fetching data from an API, updating the DOM, or subscribing to an event.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useEffect to fetch data from an API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useState, useEffect } from 'react';

export function App(props) {
  const [data, setData] = useState([]);

   useEffect(() =&amp;gt; {
    fetch('https://jsonplaceholder.typicode.com/users')
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setData(data));
  }, []);


  return (
    &amp;lt;div className='App'&amp;gt;
      &amp;lt;h1&amp;gt;Hello React.&amp;lt;/h1&amp;gt;
      &amp;lt;ul&amp;gt;
      {data.map(item =&amp;gt; (
        &amp;lt;li key={item.id}&amp;gt;{item.name}&amp;lt;/li&amp;gt;
      ))}
    &amp;lt;/ul&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we fetch data from an API and update the component’s state with it using the setData function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3: useContext hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useContext hook allows you to access a context object in a functional component. Context is a way to pass data down the component tree without having to pass props manually.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useContext to access a theme context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useContext } from 'react';

const ThemeContext = React.createContext('light');

function ThemeButton() {
  const theme = useContext(ThemeContext);

  return (
    &amp;lt;button style={{ background: theme === 'dark' ? 'black' : 'white', color: theme === 'dark' ? 'white' : 'black' }}&amp;gt;
      Toggle Theme
    &amp;lt;/button&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we create a theme context with a default value of “light” and use the useContext hook to access it in the ThemeButton component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4: useReducer hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useReducer hook allows you to manage complex state in a functional component. It’s similar to the useState hook, but instead of a simple value, it takes a reducer function and an initial state.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useReducer to manage a shopping cart:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useReducer } from 'react';

function cartReducer(state, action) {
  switch (action.type) {
    case 'add':
      return [...state, action.payload];
    case 'remove':
      return state.filter(item =&amp;gt; item.id !== action.payload.id);
    default:
      return state;
  }
}

function ShoppingCart() {
  const [cart, dispatch] = useReducer(cartReducer, []);

  const addItem = (item) =&amp;gt; {
    dispatch({ type: 'add', payload: item });
  }

  const removeItem = (item) =&amp;gt; {
    dispatch({ type: 'remove', payload: item });
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h2&amp;gt;Shopping Cart&amp;lt;/h2&amp;gt;
      &amp;lt;ul&amp;gt;
        {cart.map(item =&amp;gt; (
          &amp;lt;li key={item.id}&amp;gt;
            {item.name} - ${item.price}
            &amp;lt;button onClick={() =&amp;gt; removeItem(item)}&amp;gt;Remove&amp;lt;/button&amp;gt;
          &amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; addItem({ id: 1, name: 'Item 1', price: 9.99 })}&amp;gt;Add Item&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a cartReducer function that takes a state and an action and returns a new state based on the action type. We then use the useReducer hook to manage the cart state with the cartReducer function.&lt;/p&gt;

&lt;p&gt;We also define two functions, addItem and removeItem, that dispatch actions to the cartReducer to add or remove items from the cart state.&lt;/p&gt;

&lt;p&gt;Finally, we render the shopping cart with the cart items and buttons to add or remove items. When the buttons are clicked, the addItem or removeItem functions are called to update the cart state using the dispatch function returned by the useReducer hook.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5: useCallback hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useCallback hook allows you to memoize a function so that it’s only re-created when its dependencies change. This can help improve performance by preventing unnecessary re-renders.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useCallback to memoize a function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useCallback } from 'react';

function SearchBar({ onSearch }) {
  const [query, setQuery] = useState('');

  const handleQueryChange = useCallback(event =&amp;gt; {
    setQuery(event.target.value);
    onSearch(event.target.value);
  }, [onSearch]);

  return (
    &amp;lt;input type="text" value={query} onChange={handleQueryChange} /&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a SearchBar component that takes an onSearch prop function. We use the useCallback hook to memoize the handleQueryChange function so that it’s only re-created when the onSearch function changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6: useMemo hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useMemo hook allows you to memoize a value so that it’s only re-computed when its dependencies change. This can help improve performance by preventing unnecessary re-computations.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useMemo to memoize a calculated value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState, useMemo } from 'react';

function ExpensiveCalculation({ a, b }) {
  const result = useMemo(() =&amp;gt; {
    console.log('Calculating...');
    return a * b;
  }, [a, b]);

  return &amp;lt;p&amp;gt;Result: {result}&amp;lt;/p&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define an ExpensiveCalculation component that takes two props, a and b. We use the useMemo hook to memoize the result of the calculation so that it’s only re-computed when a or b changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7: useRef hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useRef hook allows you to create a mutable ref object that persists for the lifetime of the component. You can use it to store and access values that don’t trigger a re-render.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useRef to access the value of an input element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useRef } from 'react';

function InputWithFocus() {
  const inputRef = useRef();

  const handleClick = () =&amp;gt; {
    inputRef.current.focus();
  }

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input type="text" ref={inputRef} /&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Focus Input&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define an InputWithFocus component that creates a ref object with the useRef hook and attaches it to the input element. We use the ref object to focus the input when the “Focus Input” button is clicked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8: useLayoutEffect hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The useLayoutEffect hook is similar to useEffect, but it’s executed synchronously after all DOM mutations. This makes it useful for manipulating the DOM immediately after a component is rendered.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useLayoutEffect to measure the size of an element:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useState, useLayoutEffect, useRef } from 'react';

function ResizableBox() {
  const [width, setWidth] = useState(100);
  const [height, setHeight] = useState(100);
  const boxRef = useRef(null);

  useLayoutEffect(() =&amp;gt; {
    const handleResize = () =&amp;gt; {
      setWidth(boxRef.current.clientWidth);
      setHeight(boxRef.current.clientHeight);
    };

    handleResize();

    window.addEventListener('resize', handleResize);
    return () =&amp;gt; window.removeEventListener('resize', handleResize);
  }, []);

  return (
    &amp;lt;div ref={boxRef} style={{ width: '50%', height: '50%', backgroundColor: 'red' }}&amp;gt;
      &amp;lt;p&amp;gt;Width: {width}px&amp;lt;/p&amp;gt;
      &amp;lt;p&amp;gt;Height: {height}px&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a ResizableBox component that uses the useLayoutEffect hook to measure the size of a div element. We then use the size values to render the width and height of the box. The hook also adds and removes a resize event listener to update the size values when the window is resized.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9: useDebugValue hook&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;useDebugValue is a hook that allows you to display custom debugging information for custom hooks in the React DevTools. This can be useful for debugging hooks and understanding what's happening behind the scenes.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useDebugValue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect, useDebugValue } from 'react';

function useFetch(url) {
  const [data, setData] = useState(null);

  useEffect(() =&amp;gt; {
    fetch(url)
      .then(response =&amp;gt; response.json())
      .then(data =&amp;gt; setData(data));
  }, [url]);

  useDebugValue(data ? `Data loaded: ${data.length} items` : 'Loading...');

  return data;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a custom hook called useFetch that fetches data from a URL and returns it. We use the useDebugValue hook to display a custom debugging message in the React DevTools. If the data is loaded, we display a message that includes the number of items in the data. If the data is still loading, we display a message that says "Loading...".&lt;/p&gt;

&lt;p&gt;When you use the useFetch hook in a component, the custom debugging message will be displayed in the React DevTools. This can help you understand what's happening behind the scenes and debug any issues that might arise.&lt;/p&gt;

&lt;p&gt;Note that the useDebugValue hook should only be used for debugging purposes and should not affect the behavior of your custom hook. It's a great tool to have in your debugging toolbox when working with custom hooks.&lt;br&gt;
10: useImperativeHandle hook&lt;/p&gt;

&lt;p&gt;The useImperativeHandle hook allows you to customize the instance value that is exposed to parent components when using ref. This can be useful when you need to provide a certain interface to parent components, but you don't want to expose all of the internal implementation details of a child component.&lt;/p&gt;

&lt;p&gt;Here’s an example of how to use useImperativeHandle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useRef, useImperativeHandle } from 'react';

const Input = React.forwardRef((props, ref) =&amp;gt; {
  const inputRef = useRef();

  useImperativeHandle(ref, () =&amp;gt; ({
    focus: () =&amp;gt; {
      inputRef.current.focus();
    },
    value: inputRef.current.value
  }));

  return (
    &amp;lt;input
      type="text"
      ref={inputRef}
      placeholder={props.placeholder}
    /&amp;gt;
  );
});

function App() {
  const inputRef = useRef();

  const handleClick = () =&amp;gt; {
    inputRef.current.focus();
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Input ref={inputRef} placeholder="Type here" /&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Focus input&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we define a custom Input component that uses useImperativeHandle to expose a focus method and a value property to parent components when using ref. The useImperativeHandle hook takes two arguments: the ref object and a callback function that returns an object with the properties and methods that should be exposed to parent components.&lt;/p&gt;

&lt;p&gt;In the App component, we use the Input component and pass a ref object to it. We also define a handleClick function that calls the focus method of the inputRef object when a button is clicked.&lt;/p&gt;

&lt;p&gt;When you run this code, you’ll see an input field with a placeholder text and a button that says “Focus input”. When you click the button, the focus method of the inputRef object is called, and the input field is focused.&lt;/p&gt;

&lt;p&gt;In summary, the useImperativeHandle hook allows you to customize the instance value that is exposed to parent components when using ref. This can be useful when you need to provide a certain interface to parent components but don't want to expose all of the internal implementation details of a child component.&lt;br&gt;
Summary&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;“With these powerful React hooks in your toolkit, you can take your web development to the next level and build more efficient and effective applications than ever before.”
“From managing state with useReducer to optimizing performance with useMemo and useCallback, these React hooks are a game-changer for developers."
“Whether you’re a seasoned React developer or just starting out, these hooks can help you streamline your code, improve performance, and enhance the user experience.
“By utilizing the full potential of React hooks such as useDebugValue and useImperativeHandle, you can optimize your code and make it more maintainable and extensible."
“In summary, React hooks are a powerful tool that every developer should have in their arsenal. By incorporating these hooks into your projects, you can create better, faster, and more responsive web applications.”
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you like it then don’t forget to like and follow.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Promise of JavaScript</title>
      <dc:creator>Rohit Singh Rajput</dc:creator>
      <pubDate>Sat, 24 Aug 2024 12:10:07 +0000</pubDate>
      <link>https://dev.to/rohit413/react-acl</link>
      <guid>https://dev.to/rohit413/react-acl</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r6xkgkqfr42qb3fqme3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r6xkgkqfr42qb3fqme3.png" alt="Image description" width="800" height="729"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsyb85gqry1cm6txn92n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbsyb85gqry1cm6txn92n.png" alt="Image description" width="332" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfgbn02hwo7u9h21fw7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwfgbn02hwo7u9h21fw7f.png" alt="Image description" width="800" height="773"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
