Categories
React Answers

How to fix React input defaultValue doesn’t update with state with JavaScript?

Sometimes, we want to fix React input defaultValue doesn’t update with state with JavaScript.

In this article, we’ll look at how to fix React input defaultValue doesn’t update with state with JavaScript.

How to fix React input defaultValue doesn’t update with state with JavaScript?

To fix React input defaultValue doesn’t update with state with JavaScript, we can update the key value when the defaultValue updates.

For instance, we write

<input defaultValue={myVal} key={myVal} />;

in our component to set the defaultValue prop to myVal to set the default value of the input.

And we set the key prop to the same value so that when myVal changes, the key prop changes, which will trigger the input to re-render.

Conclusion

To fix React input defaultValue doesn’t update with state with JavaScript, we can update the key value when the defaultValue updates.

Categories
JavaScript Answers

How to concatenate with variable in a JavaScript regex pattern?

Sometimes, we want to concatenate with variable in a JavaScript regex pattern.

In this article, we’ll look at how to concatenate with variable in a JavaScript regex pattern.

How to concatenate with variable in a JavaScript regex pattern?

To concatenate with variable in a JavaScript regex pattern, we can use the RegExp constructor to create our regex.

For instance, we write

const re = new RegExp("\\b" + test + "\\b");

to create a regex object with the RegExp constructor.

We call it with a string with the regex pattern we want to define.

Since the argument is a string, we can concatenate values in it.

Conclusion

To concatenate with variable in a JavaScript regex pattern, we can use the RegExp constructor to create our regex.

Categories
JavaScript Answers

How to add simple throttling in JavaScript?

Sometimes, we want to add simple throttling in JavaScript.

In this article, we’ll look at how to add simple throttling in JavaScript.

How to add simple throttling in JavaScript?

To add simple throttling in JavaScript, we can create our own function.

For instance, we write

const throttle = (func, timeFrame) => {
  let lastTime = 0;
  return () => {
    const now = Date.now();
    if (now - lastTime >= timeFrame) {
      func();
      lastTime = now;
    }
  };
};

to define the throttle function that takes the func function and the timeFrame in milliseconds that func is allowed to run once.

In it, we return a function that checks now - lastTime is bigger than or equal to timeFrame.

If it is, then we call func since it hasn’t been called within the timeFrame.

And then we set lastTime to now.

now is the current datetime’s timestamp in milliseconds.

Conclusion

To add simple throttling in JavaScript, we can create our own function.

Categories
JavaScript Answers

How to subtract days, months, years from a date in JavaScript?

Sometimes, we want to subtract days, months, years from a date in JavaScript.

In this article, we’ll look at how to subtract days, months, years from a date in JavaScript.

How to subtract days, months, years from a date in JavaScript?

To subtract days, months, years from a date in JavaScript, we can call setDate, setMonth, and setFullYear.

For instance, we write

const createDate = (days, months, years) => {
  const date = new Date();
  date.setDate(date.getDate() + days);
  date.setMonth(date.getMonth() + months);
  date.setFullYear(date.getFullYear() + years);
  return date;
};

to define the createDate function.

In it, we create a Date object.

We call setDate with the date value with days added to it to add the number of days to the current date, which we get from getDate.

We call setMonth with the date value with days added to it to add the number of months to the current date, which we get from getMonth.

And we call setFullYear with the date value with days added to it to add the number of years to the current date, which we get from getFullYear.

Finally, we return the date with the values added.

We call createDate with negative values for each argument to subtract them.

Conclusion

To subtract days, months, years from a date in JavaScript, we can call setDate, setMonth, and setFullYear.

Categories
JavaScript Answers

How to set image source with base64 with JavaScript?

Sometimes, we want to set image source with base64 with JavaScript.

In this article, we’ll look at how to set image source with base64 with JavaScript.

How to set image source with base64 with JavaScript?

To set image source with base64 with JavaScript, we can set the src property to a base64 URL string.

For instance, we write

document.getElementById("img").src =
  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

to select the img element with getElementById.

Then we set its src property to the base64 image URL string to set the src attribute of the img element to that value.

Conclusion

To set image source with base64 with JavaScript, we can set the src property to a base64 URL string.