Categories
JavaScript Answers

How to show a pop-up print dialog box using JavaScript?

Sometimes, we want to show a pop-up print dialog box using JavaScript.

In this article, we’ll look at how to show a pop-up print dialog box using JavaScript.

How to show a pop-up print dialog box using JavaScript?

To show a pop-up print dialog box using JavaScript, we can use the window.print method.

For instance, we write

window.print();  

to open the print dialog box of the browser.

Conclusion

To show a pop-up print dialog box using JavaScript, we can use the window.print method.

Categories
JavaScript Answers

How to fix JavaScript Date.getDay() returns wrong day?

Sometimes, we want to fix JavaScript Date.getDay() returns wrong day.

In this article, we’ll look at how to fix JavaScript Date.getDay() returns wrong day.

How to fix JavaScript Date.getDay() returns wrong day?

To fix JavaScript Date.getDay() returns wrong day, we use the date’s getDate method to get the day of the month.

For instance, we write

const date = new Date().getDate();

to get the date of the month of today’s date with getDate.

Conclusion

To fix JavaScript Date.getDay() returns wrong day, we use the date’s getDate method to get the day of the month.

Categories
JavaScript Answers

How to get height of an element with React?

Sometimes, we want to get height of an element with React.

In this article, we’ll look at how to get height of an element with React.

How to get height of an element with React?

To get height of an element with React, we can use the useEffect hook.

For instance, we write

import React, { useState, useEffect, useRef } from "react";

export default () => {
  const [height, setHeight] = useState(0);
  const ref = useRef(null);

  useEffect(() => {
    setHeight(ref.current.clientHeight);
  });

  return <div ref={ref}>{height}</div>;
};

to define the height state with the useState hook.

And then we define a ref that we assign to the div.

Then we call useEffect with a callback to call setHeight with the height of the div.

We called it with no 2nd argument so the useEffect callback runs on every re-render to update the height.

Conclusion

To get height of an element with React, we can use the useEffect hook.

Categories
JavaScript Answers

How to get HTML5 localStorage keys with JavaScript?

Sometimes, we want to get HTML5 localStorage keys with JavaScript.

In this article, we’ll look at how to get HTML5 localStorage keys with JavaScript.

How to get HTML5 localStorage keys with JavaScript?

To get HTML5 localStorage keys with JavaScript, we can use the Object.entries method.

For instance, we write

const entries = Object.entries(localStorage);

to call Object.entries with localStorage to return an array with the key-value pair arrays in localStorage.

Conclusion

To get HTML5 localStorage keys with JavaScript, we can use the Object.entries method.

Categories
JavaScript Answers

How to disable console inside unit tests with Jest?

Sometimes, we want to disable console inside unit tests with Jest.

In this article, we’ll look at how to disable console inside unit tests with Jest.

How to disable console inside unit tests with Jest?

To disable console inside unit tests with Jest, we can use the --silent option or set the console methods to mocked functions.

For instance, we run

jest --silent

to run jest with the --silient to disable console output when running tests.

Or we can create a tests/setup.js file with

global.console = {
  ...console,
  log: jest.fn(),
  debug: jest.fn(),
  info: jest.fn(),
  warn: jest.fn(),
  error: jest.fn(),
};

to set all the console methods to mocked functions.

And then we can use the config by writing

module.exports = {
  verbose: true,
  setupFilesAfterEnv: ["<rootDir>/__tests__/setup.js"],
};

to add tests/setup.js to the setupFilesAfterEnv array in jest.config.js to load that when running tests.

Conclusion

To disable console inside unit tests with Jest, we can use the --silent option or set the console methods to mocked functions.