Posts

React Logo

How to get yesterday’s date in React JS?

< 1 min readYou can get yesterday’s date in React using the JavaScript Date object. Here’s how you can do it: In this example, the getYesterdayDate function creates a new Date object for today, then creates another Date object for yesterday by subtracting one day from the current date using setDate(). After that, it formats the yesterday’s date…

Read More
React Logo

How to scroll page to the bottom in React JS?

< 1 min readTo scroll the page to the bottom in a React component, you can use the window.scrollTo() method or use the scrollIntoView() method on a specific element. Here’s how you can do both: Using window.scrollTo(): In this example, clicking the “Scroll to Bottom” button will smoothly scroll the page to the bottom using the window.scrollTo() method….

Read More
React Logo

How to detect dark mode in React JS?

< 1 min readTo detect dark mode in a React application, you can use the window.matchMedia API with the prefers-color-scheme media query, similar to how you would do it in regular JavaScript. Here’s an example of how you can achieve this: Create a new React component or modify an existing one. Use the useEffect hook to listen for…

Read More
React Logo

How to get the current URL in React?

< 1 min readIn a React component, you can get the current URL using the window.location object. Here’s how you can access the current URL: In this example, window.location.href provides the complete URL of the current page, including the protocol, domain, path, query parameters, and hash. Please note that directly using window.location in your React component may not…

Read More
React Logo

How to go to the previous page in React?

< 1 min readIn a React application, you can navigate to the previous page using the useHistory hook from the react-router-dom library. This library provides tools for managing routing within a React application. Here’s how you can use it to go back to the previous page: Install the react-router-dom package if you haven’t already: In your React component,…

Read More
Javascript Logo

How to detect dark mode in Javascript?

< 1 min readYou can detect whether the user’s operating system or device is currently in dark mode by checking the prefers-color-scheme media query in JavaScript. This media query provides information about the user’s preference for light or dark color schemes. Here’s how you can use it: In this example, the isDarkMode() function checks if the user’s preference…

Read More
Javascript Logo

How to generate random colors in Javascript?

< 1 min readYou can generate a random color in JavaScript by generating random values for the red, green, and blue components of the color. Here’s a simple example using the Math.random() function: This code generates random values for red, green, and blue components and combines them into an RGB color format. If you prefer using hexadecimal color…

Read More