Categories
JavaScript Answers

How to load and execute external JavaScript file in Node.js with access to local variables?

Sometimes, we want to load and execute external JavaScript file in Node.js with access to local variables.

In this article, we’ll look at how to load and execute external JavaScript file in Node.js with access to local variables.

How to load and execute external JavaScript file in Node.js with access to local variables?

To load and execute external JavaScript file in Node.js with access to local variables, we can use the vm module.

For instance, we write

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

const data = fs.readFileSync("./externalfile.js");
const script = new vm.Script(data);
script.runInThisContext();

to read the externfile.js JavaScript file with readFileSync.

Then we create a vm.Script object with the read file.

Next, we call runInThisContext to run the script file.

Conclusion

To load and execute external JavaScript file in Node.js with access to local variables, we can use the vm module.

Categories
HTML

How to download a PDF file instead of opening them in browser when clicked?

Sometimes, we want to download a PDF file instead of opening them in browser when clicked.

In this article, we’ll look at how to download a PDF file instead of opening them in browser when clicked.

How to download a PDF file instead of opening them in browser when clicked?

To download a PDF file instead of opening them in browser when clicked, we can add the download attribute to the link.

For instance, we write

<a href="http://link/to/file" download="FileName">Download it!</a>

to set the download attribute of the anchor element to the file name of file when we download it.

Then when we click on the link, the file will be downloaded.

Conclusion

To download a PDF file instead of opening them in browser when clicked, we can add the download attribute to the link.

Categories
JavaScript Answers

How to define multiple variables on a single line with JavaScript?

Sometimes, we want to define multiple variables on a single line with JavaScript.

In this article, we’ll look at how to define multiple variables on a single line with JavaScript.

How to define multiple variables on a single line with JavaScript?

To define multiple variables on a single line with JavaScript, we can use the array destructuring syntax.

For instance, we write

const [a, b, c, d] = [0, 1, 2, 3];

to assign a to 0, b to 1, cto 2, andd` to 3 with the array destructuring syntax.

Conclusion

To define multiple variables on a single line with JavaScript, we can use the array destructuring syntax.

Categories
Vue Answers

How to pass a parameter to Vue @click event handler?

Sometimes, we want to pass a parameter to Vue @click event handler.

In this article, we’ll look at how to pass a parameter to Vue @click event handler.

How to pass a parameter to Vue @click event handler?

To pass a parameter to Vue @click event handler, we pass the arguments straight into the click event handler.

For instance, we write

<template>
  <table>
    <tr
      v-for="item in items"
      :key="item.contactID"
      @click="addToCount(item.contactID)"
    >
      <td>{{ item.contactName }}</td>
      <td>{{ item.recipient }}</td>
    </tr>
  </table>
</template>

<script>
//...
export default {
  methods: {
    addToCount(paramContactID) {
      //...
    },
  },
};
</script>

to set the click event handler to addToCount on the tr element.

We set it to call addToCount with the item.contactID value as its argument.

And we get the item.contactID value from paramContactID.

Conclusion

To pass a parameter to Vue @click event handler, we pass the arguments straight into the click event handler.

Categories
React Answers

How to wait for setState to finish before triggering a function with React.js?

Sometimes, we want to wait for setState to finish before triggering a function with React.js.

In this article, we’ll look at how to wait for setState to finish before triggering a function with React.js.

How to wait for setState to finish before triggering a function with React.js?

To wait for setState to finish before triggering a function with React.js, we call setState with a callback that runs after the state is updated as the 2nd argument.

For instance, we write

this.setState(
  {
    originId: input.originId,
    destinationId: input.destinationId,
    radius: input.radius,
    search: input.search,
  },
  this.findRoutes
);

to call setState with an object to update the states.

The 2nd argument is the this.findRoutes method that runs when we finished updating the states.

We call this.setState in a method to update the states.

Conclusion

To wait for setState to finish before triggering a function with React.js, we call setState with a callback that runs after the state is updated as the 2nd argument.