Categories
JavaScript Answers

How to fix Destructuring of undefined error with JavaScript?

Sometimes, we want to fix Destructuring of undefined error with JavaScript.

In this article, we’ll look at how to fix Destructuring of undefined error with JavaScript.

How to fix Destructuring of undefined error with JavaScript?

To fix Destructuring of undefined error with JavaScript, we use an empty object as the fallback value.

For instance, we write

const content = undefined;
const { item } = content || {};
console.log(item);

to use {} as the fallback value for content.

Then item is undefined since content is undefined and the empty object is destructured.

Conclusion

To fix Destructuring of undefined error with JavaScript, we use an empty object as the fallback value.

Categories
JavaScript Answers

How to draw smooth curve through N points using JavaScript HTML5 canvas?

Sometimes, we want to draw smooth curve through N points using JavaScript HTML5 canvas.

In this article, we’ll look at how to draw smooth curve through N points using JavaScript HTML5 canvas.

How to draw smooth curve through N points using JavaScript HTML5 canvas?

To draw smooth curve through N points using JavaScript HTML5 canvas, we create a loop that calls the canvas context quandraticCurveTo method with the end and midpoints.

For instance, we write

const points = [
  { x: 1, y: 1 },
  { x: 2, y: 3 },
  { x: 3, y: 4 },
  { x: 4, y: 2 },
  { x: 5, y: 6 },
];
ctx.moveTo(points[0].x, points[0].y);

for (let i = 0; i < points.length - 1; i++) {
  const xMid = (points[i].x + points[i + 1].x) / 2;
  const yMid = (points[i].y + points[i + 1].y) / 2;
  const cpX1 = (xMid + points[i].x) / 2;
  const cpX2 = (xMid + points[i + 1].x) / 2;
  ctx.quadraticCurveTo(cpX1, points[i].y, xMid, yMid);
  ctx.quadraticCurveTo(cpX2, points[i + 1].y, points[i + 1].x, points[i + 1].y);
}

to create a for loop that gets the midpoint x and y coordinates with

const xMid = (points[i].x + points[i + 1].x) / 2;
const yMid = (points[i].y + points[i + 1].y) / 2;

We get the x coordinate endpoints with

const cpX1 = (xMid + points[i].x) / 2;
const cpX2 = (xMid + points[i + 1].x) / 2;

And then we call quadraticCurveTo method to draw the point from the current point to midpoint.

Then we call quadraticCurveTo again to draw the curve between the midpoint between the midpoint and the next point and the next point.

Conclusion

To draw smooth curve through N points using JavaScript HTML5 canvas, we create a loop that calls the canvas context quandraticCurveTo method with the end and midpoints.

Categories
JavaScript Answers

How to fix input losing focus when rerendering with React?

Sometimes, we want to fix input losing focus when rerendering with React.

In this article, we’ll look at how to fix input losing focus when rerendering with React.

How to fix input losing focus when rerendering with React?

To fix input losing focus when rerendering with React, we should define child components outside the parent component.

For instance, we write

const Child = () => <p>Child!</p>;
const Parent = () => <Child />;

to define the Child component outside the Parent.

And then we render the Child component in the Parent.

This way, when Parent re-renders, Child only re-renders if the props or state of it changes.

Conclusion

To fix input losing focus when rerendering with React, we should define child components outside the parent component.

Categories
JavaScript Answers

How to execute function after complete page load with JavaScript?

Sometimes, we want to execute function after complete page load with JavaScript.

In this article, we’ll look at how to execute function after complete page load with JavaScript.

How to execute function after complete page load with JavaScript?

To execute function after complete page load with JavaScript, we listen to the readystatechange event.

For instance, we write

document.addEventListener("readystatechange", (event) => {
  if (event.target.readyState === "interactive") {
    console.log("hi");
  }

  if (event.target.readyState === "complete") {
    console.log("hi 2");
  }
});

to listen to the document‘s readystatechange event with addEventListener.

In the event listener callback, we get the document‘s readyState with event.target.readyState.

If it’s 'interactive' then the page can be interacted with but it’s still loading.

And if the readyState is 'complete', then the page is completely loaded.

Conclusion

To execute function after complete page load with JavaScript, we listen to the readystatechange event.

Categories
JavaScript Answers

How to implement prepend and append elements with regular JavaScript?

Sometimes, we want to implement prepend and append elements with regular JavaScript.

In this article, we’ll look at how to implement prepend and append elements with regular JavaScript.

How to implement prepend and append elements with regular JavaScript?

To implement prepend and append elements with regular JavaScript, we use appendChild to append and insertBefore to prepend.

For instance, we write

const theParent = document.getElementById("theParent");
const theKid = document.createElement("div");
theKid.innerHTML = "Are we there yet?";

theParent.appendChild(theKid);
theParent.insertBefore(theKid, theParent.firstChild);

to get the theParent element and create the theKid element with createElement.

Then we call theParent.appendChild with theKid to append the theKid as the last child of theParent.

And we call theParent.insertBefore with theKid and theParent.firstChild to prepend theKid as the element before the first child element of theParent.

Conclusion

To implement prepend and append elements with regular JavaScript, we use appendChild to append and insertBefore to prepend.