Categories
JavaScript Answers

What’s the equivalent of jQuery $(window).on(‘resize’) in JavaScript?

Sometimes, we want to find the equivalent of jQuery $(window).on('resize') in JavaScript.

In this article, we’ll look at the equivalent of jQuery $(window).on('resize') in JavaScript.

What’s the equivalent of jQuery $(window).on(‘resize’) in JavaScript?

The equivalent of jQuery $(window).on('resize') in JavaScript is:

window.addEventListener('resize', () => {})

We call window.addEventListener with 'resize' and a callback that runs when the window is resized to add an event listener for the resize event.

The resize event is triggered when we resize the window.

Conclusion

The equivalent of jQuery $(window).on('resize') in JavaScript is:

window.addEventListener('resize', () => {})
Categories
JavaScript Answers

How to fix the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error with JavaScript?

Sometimes, we need to fix the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error with JavaScript.

In this article, we’ll look at how to fix the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error with JavaScript.

How to fix the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error with JavaScript?

To fix the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error with JavaScript, we should make sure the value prop always has a value set.

For instance, we write:

import React from "react";

export default function App() {
  const [name, setName] = React.useState();

  return (
    <div>
      <input value={name ?? ""} onChange={(e) => setName(e.target.value)} />
    </div>
  );
}

to define the name state with useState and didn’t set an initial value for name.

Then we add the input with the value prop set to name.

If name isn’t defined, we set it to an empty string.

As a result, the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error won’t be thrown.

Conclusiun

To fix the ‘React a component is changing an uncontrolled input of type checkbox to be controlled’ error with JavaScript, we should make sure the value prop always has a value set.

Categories
JavaScript Answers

How to convert a JSON object to a JavaScript class instance?

Sometimes, we want to convert a JSON object to a JavaScript class instance.

In this article, we’ll look at how to convert a JSON object to a JavaScript class instance.

How to convert a JSON object to a JavaScript class instance?

To convert a JSON object to a JavaScript class instance, we can merge the content of the JSON object into this in the constructor.

For instance, we write:

class Student {
  constructor(json) {
    Object.assign(this, json);
  }
}
const student = new Student({
  firstName: "jane"
});
console.log(student)

to define the Student class.

In the constructor, we call Object.assign with this and json to merge the json into this.

Then we create a new Student instance and return it.

Therefore, student is {firstName: 'jane'}.

Conclusion

To convert a JSON object to a JavaScript class instance, we can merge the content of the JSON object into this in the constructor.

Categories
JavaScript Answers

How to generate random hex string in JavaScript?

Sometimes, we want to generate random hex string in JavaScript.

In this article, we’ll look at how to generate random hex string in JavaScript.

How to generate random hex string in JavaScript?

To generate random hex string in JavaScript, we can use the Math.random and toString methods.

For instance, we write:

const genRanHex = size => [...Array(size)]
  .map(() => Math.floor(Math.random() * 16).toString(16)).join('');

console.log(genRanHex(6));
console.log(genRanHex(8));

to define the genRanHex function to generate the hex number.

In the function, we create an array with the given size and make a copy of it with [...Array(size)].

Then we call map with a callback that returns the randomly generated hex number with Math.floor(Math.random() * 16).toString(16).

We call toString with 16 to convert the randomly generated decimal number to hex.

Finally, we call join with an empty string to combine them together.

Conclusion

To generate random hex string in JavaScript, we can use the Math.random and toString methods.

Categories
JavaScript Answers

How to convert a string to an XML Document in JavaScript?

Sometimes, we want to convert a string to an XML Document in JavaScript.

In this article, we’ll look at how to convert a string to an XML Document in JavaScript.

How to convert a string to an XML Document in JavaScript?

To convert a string to an XML Document in JavaScript, we can use the DOMParser constructor.

For instance, we write:

const xmlString = '<foo><bar>something</bar></foo>';
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");
console.log(xmlDoc)

We create a new DOMParser instance.

And then we call parseFromString with the xmlString and the 'application/xml' MIME type to parse the string as XML.

Therefore, xmlDoc is an XML document converted from the string.

Conclusion

To convert a string to an XML Document in JavaScript, we can use the DOMParser constructor.