Categories
JavaScript Answers

How to detect long press in JavaScript?

Sometimes, we want to detect long press in JavaScript.

In this article, we’ll look at how to detect long press in JavaScript.

How to detect long press in JavaScript?

To detect long press in JavaScript, we can listen for the contextmenu event.

For instance, we write

document.addEventListener('contextmenu', callback);

to call addEventListener to listen for the contextmenu event.

The callback function is called when when the contextmenu event is triggered by long presses.

Conclusion

To detect long press in JavaScript, we can listen for the contextmenu event.

Categories
JavaScript Answers

How to download a file using window.fetch?

Sometimes, we want to download a file using window.fetch.

In this article, we’ll look at how to download a file using window.fetch.

How to download a file using window.fetch?

To download a file using window.fetch, we can call the response object’s blob method.

For instance, we write

const url = "http://example.com/file.doc";
const authHeader = "... ";

const options = {
  headers: {
    Authorization: authHeader,
  },
};
const res = await fetch(url, options);
const blob = await res.blob();
const file = window.URL.createObjectURL(blob);
window.location.assign(file);

to call fetch with the url with the file.

options has the headers for the request.

Then we call res.blob to get the content of the file as a blob.

Next, we call window.URL.createObjectURL with blob to create the file.

And then we call window.location.assign with file to download the file.

Conclusion

To download a file using window.fetch, we can call the response object’s blob method.

Categories
JavaScript Answers

How to parse large JSON file in Node.js?

Sometimes, we want to parse large JSON file in Node.js.

In this article, we’ll look at how to parse large JSON file in Node.js.

How to parse large JSON file in Node.js?

To parse large JSON file in Node.js, we call fs.createReadStream and use the JSONStream library.

To install it, we run

npm i JSONStream

Then, we write

const fs = require("fs");
const JSONStream = require("JSONStream");

const getStream = () => {
  const jsonData = "myData.json";
  const stream = fs.createReadStream(jsonData, { encoding: "utf8" });
  const parser = JSONStream.parse("*");
  return stream.pipe(parser);
};

getStream()
  .pipe(transform)
  .on("error", (err) => {
    // ...
  });

to call fs.createReadStream to read the file at path jsonData.

And then we call JSONStream.parse to create a parser object.

Next, we call stream.pipe with parser to parse the JSON that’s read from the stream.

Then we call getStream with pipe to transform the result with the transform function.

And we watch for errors with on.

Conclusion

To parse large JSON file in Node.js, we call fs.createReadStream and use the JSONStream library.

Categories
React Answers

How to add onClick event handler with React?

Sometimes, we want to add onClick event handler with React.

In this article, we’ll look at how to add onClick event handler with React.

How to add onClick event handler with React?

To add onClick event handler with React, we set the onClick prop of an element to a function that runs when we click on the element.

For instance, we write

const ActionLink = () => {
  const handleClick = (e) => {
    e.preventDefault();
    console.log("The link was clicked.");
  };

  return (
    <a href="#" onClick={handleClick}>
      Click me
    </a>
  );
};

to set the onClick prop of the a element to the handleClick function.

In handleClick, we call e.preventDefault to prevent the default action of navigating to the URL set as the value of the href attribute.

Then we call console.log to log some text.

Conclusion

To add onClick event handler with React, we set the onClick prop of an element to a function that runs when we click on the element.

Categories
JavaScript Answers

How to call JavaScript parseInt() with strings with leading zeros?

Sometimes, we want to call JavaScript parseInt() with strings with leading zeros.

In this article, we’ll look at how to call JavaScript parseInt() with strings with leading zeros.

How to call JavaScript parseInt() with strings with leading zeros?

To call JavaScript parseInt() with strings with leading zeros, we call it with 10 as the 2nd argument.

For instance, we write

const n = parseInt("09", 10);

to call parseInt with '09' and 10 to parse '09' into a decimal number.

Therefore n is 9.

Conclusion

To call JavaScript parseInt() with strings with leading zeros, we call it with 10 as the 2nd argument.