Categories
JavaScript Answers

How to get month in mm format in JavaScript?

Sometimes, we want to get month in mm format in JavaScript.

In this article, we’ll look at how to get month in mm format in JavaScript.

How to get month in mm format in JavaScript?

To get month in mm format in JavaScript, we can use the date’s getMonth and string’s padStart methods.

For instance, we write:

const mm = (new Date(2022, 1, 1).getMonth() + 1).toString().padStart(2, '0')
console.log(mm)

to create a new date set to Feb 1, 2022.

We get the month number from the date with getMonth

And we add 1 to it to convert it to a human readable month number.

Then we call toString to convert it to a string.

And then we all padStart with 2 and '0' to prepend 0’s to the string until it has length 2.

Therefore, mm is '02'.

Conclusion

To get month in mm format in JavaScript, we can use the date’s getMonth and string’s padStart methods.

Categories
JavaScript Answers

How to check if a div element is empty with JavaScript?

Sometimes, we want to check if a div element is empty with JavaScript.

In this article, we’ll look at how to check if a div element is empty with JavaScript.

How to check if a div element is empty with JavaScript?

To check if a div element is empty with JavaScript, we can check if the innerHTML property of the div is empty.

For instance, we write:

<div></div>

to add an empty div.

Then we write:

const isEmpty = document.querySelector('div').innerHTML === "";
console.log(isEmpty)

to select the div with querySelector.

Then we check if its innerHTML property is an empty string.

If it is, then the div is empty.

Since, isEmpty logs true, the div is empty.

Conclusion

To check if a div element is empty with JavaScript, we can check if the innerHTML property of the div is empty.

Categories
JavaScript Answers

How to generate an image of a div and save the image to disk with JavaScript?

Sometimes, we want to generate an image of a div and save the image to disk with JavaScript.

In this article, we’ll look at how to generate an image of a div and save the image to disk with JavaScript.

How to generate an image of a div and save the image to disk with JavaScript?

To generate an image of a div and save the image to disk with JavaScript, we can use the html2canvas library.

For instance, we write:

<script src='https://html2canvas.hertzen.com/dist/html2canvas.min.js'></script>

<div id="capture" style="padding: 10px; background: #f5da55">
  <h4 style="color: #000; ">Hello world!</h4>
</div>

to add the html2canvas script and a div that we want to capture and download as an image.

Then we write:

(async () => {
  const canvas = await html2canvas(document.querySelector("#capture"))
  const link = document.createElement("a");
  link.download = 'capture.png';
  link.href = canvas.toDataURL("image/png");
  link.click();
  link.remove()
})()

to put the div content into a canvas and return it as a promise with html2canvas.

Then we create a link with createElement.

We set the file name of the downloaded file by setting the download propert.

Next, we set the URL of the file to download to the image with canvas.toDataURL.

And then we call click to start the download.

Finally, we call remove to remove the link.

Conclusion

To generate an image of a div and save the image to disk with JavaScript, we can use the html2canvas library.

Categories
JavaScript Answers

How to detect Shift + Enter press in React onKeyPress event?

Sometimes, we want to detect Shift + Enter press in React onKeyPress event.

In this article, we’ll look at how to detect Shift + Enter press in React onKeyPress event.

How to detect Shift + Enter press in React onKeyPress event?

To detect Shift + Enter press in React onKeyPress event, we can check if shift+enter is pressed from the keypress event object.

For instance, we write:

import React from "react";

export default function App() {
  const handleKeyPress = (e) => {
    if (e.key === "Enter" && e.shiftKey) {
      console.log("pressed shift+enter");
    }
  };

  return (
    <div>
      <input onKeyPress={handleKeyPress} />
    </div>
  );
}

to set the onKeyPress prop of the input to the handleKeyPress function.

We check if e.key is 'Enter' and e.shiftKey is true to check if shift+enter is pressed.

Therefore, if we press both keys together, we see "pressed shift+enter" logged.

Conclusion

To detect Shift + Enter press in React onKeyPress event, we can check if shift+enter is pressed from the keypress event object.

Categories
JavaScript Answers React Answers

How to fix the ‘Function components cannot have refs. Did you mean to use React.forwardRef()?’ error with React and JavaScript?

Sometimes, we want to fix the ‘Function components cannot have refs. Did you mean to use React.forwardRef()?’ error with React and JavaScript.

In this article, we’ll look at how to fix the ‘Function components cannot have refs. Did you mean to use React.forwardRef()?’ error with React and JavaScript.

How to fix the ‘Function components cannot have refs. Did you mean to use React.forwardRef()?’ error with React and JavaScript?

To fix the ‘Function components cannot have refs. Did you mean to use React.forwardRef()?’ error with React and JavaScript, we should call React.forwardRef with the function component that we want to assign the ref to.

For instance, we write:

import React from "react";

const Input = React.forwardRef(({ backgroundColor }, ref) => {
  return <input style={{ backgroundColor }} ref={ref} />;
});

export default function App() {
  const ref = React.useRef();

  React.useEffect(() => {
    ref.current.focus();
  }, []);

  return (
    <div>
      <Input backgroundColor="green" ref={ref} />
    </div>
  );
}

to create the Input function component and assign a ref to it.

We set the ref prop to the ref parameter in the Input component.

Then in App, we create a ref with the useRef hook and assign the ref prop to the ref.

And we call ref.current.focus in the useEffect callback.

Now the input should be focused when we load the page.

Conclusion

To fix the ‘Function components cannot have refs. Did you mean to use React.forwardRef()?’ error with React and JavaScript, we should call React.forwardRef with the function component that we want to assign the ref to.