Categories
Vue Answers

How to get the DOM element to corresponding Vue.js component with JavaScript?

Sometimes, we want to get the DOM element to corresponding Vue.js component with JavaScript.

In this article, we’ll look at how to get the DOM element to corresponding Vue.js component with JavaScript.

How to get the DOM element to corresponding Vue.js component with JavaScript?

To get the DOM element to corresponding Vue.js component with JavaScript, we can use refs.

For instance, we write

<template>
  <div>
    root element
    <div ref="childElement">child element</div>
  </div>
</template>

<script>
export default {
  mounted() {
    const rootElement = this.$el;
    const childElement = this.$refs.childElement;

    console.log(rootElement);
    console.log(childElement);
  },
};
</script>

to assign the childElement ref to the div.

Then in the mounted hook, we get the root element of the component with this.$el.

And we get the div with this.$refs.childElement.

Conclusion

To get the DOM element to corresponding Vue.js component with JavaScript, we can use refs.

Categories
JavaScript Answers

How to fetch an array of URLs with Promise.all with JavaScript?

Sometimes, we want to fetch an array of URLs with Promise.all with JavaScript.

In this article, we’ll look at how to fetch an array of URLs with Promise.all with JavaScript.

How to fetch an array of URLs with Promise.all with JavaScript?

To fetch an array of URLs with Promise.all with JavaScript, we call Promise.all with an array of promises.

For instance, we write

const texts = await Promise.all(
  urls.map(async (url) => {
    const resp = await fetch(url);
    return resp.text();
  })
);

to call urls.map with an async function that calls fetch and returns the response as the resolve value of the promise returned.

Then we call Promise.all with the array of promises returned by map to make the requests.

Next we get the responses from the texts array which we get from using await on the promise returned by Promise.all.

Conclusion

To fetch an array of URLs with Promise.all with JavaScript, we call Promise.all with an array of promises.

Categories
JavaScript Answers

How to remove line breaks from start and end of string with JavaScript?

Sometimes, we want to remove line breaks from start and end of string with JavaScript.

In this article, we’ll look at how to remove line breaks from start and end of string with JavaScript.

How to remove line breaks from start and end of string with JavaScript?

To remove line breaks from start and end of string with JavaScript, we can use the string replace method.

For instance, we write

const newwStr = str.replace(/^\s+|\s+$/g, "");

to call replace to remove line breaks from the start and end of the string by replacing them with empty strings.

^\s+ matches the line break at the start of the string.

\s+$ matches the line break at the end of the string.

Conclusion

To remove line breaks from start and end of string with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to run Node.js app with ES6 features enabled?

Sometimes, we want to run Node.js app with ES6 features enabled.

In this article, we’ll look at how to run Node.js app with ES6 features enabled.

How to run Node.js app with ES6 features enabled?

To run Node.js app with ES6 features enabled, we install a few packages.

We enable ES6 features by running

npm install --save-dev babel
npm install --save-dev babel-cli
npm install --save-dev babel-preset-es2015
npm install --save-dev babel-preset-stage-0
npm install --save-dev nodemon

to install Babel and nodemon.

Then we create a .babelrc file in the project root and add

{
  "presets": ["es2015", "stage-0"]
}

to let us run our project with ES6 features.

Then in package.json, we write

{
  //...
  "scripts": {
    "watch": "babel -w src/ -d dist/",
    "build": "babel src/ -d dist/",
    "serve": "babel -w src/ -d dist/ | nodemon --watch dist",
    "test": "echo \"Error: no test specified\" && exit 1"
  }
  //...
}

to run babel with nodemon to let us run our Node.js app with Babel.

Conclusion

To run Node.js app with ES6 features enabled, we install a few packages.

Categories
React Answers

How to get a div’s offsetTop positions in React?

Sometimes, we want to get a div’s offsetTop positions in React.

In this article, we’ll look at how to get a div’s offsetTop positions in React.

How to get a div’s offsetTop positions in React?

To get a div’s offsetTop positions in React, we can get it from the ref assigned to the element.

For instance, we write

import { useRef } from "react";

function Component() {
  const inputRef = useRef();

  return (
    <>
      <input ref={inputRef} />
      <div
        onScroll={() => {
          const { offsetTop } = inputRef.current;
          //...
        }}
      >
        ...
      </div>
    </>
  );
}

to assign the inputRef to the input.

Then we get the offsetTop property from the inputRef.current property which has the input element.

Conclusion

To get a div’s offsetTop positions in React, we can get it from the ref assigned to the element.