Categories
JavaScript Answers

How to get image preview before uploading in React?

Sometimes, we want to get image preview before uploading in React.

In this article, we’ll look at how to get image preview before uploading in React.

How to get image preview before uploading in React?

To get image preview before uploading in React, we read the selected file into a base64 URL string.

For instance, we write

const App = () => {
  const [selectedImage, setSelectedImage] = useState();

  const imageChange = (e) => {
    if (e.target.files && e.target.files.length > 0) {
      setSelectedImage(e.target.files[0]);
    }
  };

  const removeSelectedImage = () => {
    setSelectedImage();
  };

  return (
    <>
      <div>
        <input accept="image/*" type="file" onChange={imageChange} />

        {selectedImage && (
          <div>
            <img src={URL.createObjectURL(selectedImage)} alt="Thumb" />
            <button onClick={removeSelectedImage}>Remove This Image</button>
          </div>
        )}
      </div>
    </>
  );
};

to add a file input and an img element.

We set the file input’s onChange prop to the imageChange function.

In it, we get the selected file e.target.files[0] and set that as the value of the selectedImage state.

And then we convert that to a base64 URL with URL.createObjectURL.

Conclusion

To get image preview before uploading in React, we read the selected file into a base64 URL string.

Categories
JavaScript Answers

How to convert date and time to Unix timestamp with JavaScript?

Sometimes, we want to convert date and time to Unix timestamp with JavaScript.

In this article, we’ll look at how to convert date and time to Unix timestamp with JavaScript.

How to convert date and time to Unix timestamp with JavaScript?

To convert date and time to Unix timestamp with JavaScript, we use the getTime method.

For instance, we write

const unixtime = Date.parse("24-Nov-2022 17:57:35").getTime() / 1000;

to parse the datetime string with Date.parse.

Then we call getTime to return the Unix timestamp in milliseconds.

And then we divide that by 1000 to get the equivalent in seconds.

Conclusion

To convert date and time to Unix timestamp with JavaScript, we use the getTime method.

Categories
JavaScript Answers

How to clear all intervals with JavaScript?

Sometimes, we want to clear all intervals with JavaScript.

In this article, we’ll look at how to clear all intervals with JavaScript.

How to clear all intervals with JavaScript?

To clear all intervals with JavaScript, we call clearInterval on all timer IDs.

For instance, we write

const intervalId = window.setInterval(() => {}, Number.MAX_SAFE_INTEGER);

for (let i = 1; i < intervalId; i++) {
  window.clearInterval(i);
}

to call setInterval to return a timer ID number.

Then we use a for loop to loop from 1 to intervalId and call clearInterval on all ID i.

Conclusion

To clear all intervals with JavaScript, we call clearInterval on all timer IDs.

Categories
JavaScript Answers

How to do JavaScript string or integer comparisons?

Sometimes, we want to do JavaScript string or integer comparisons.

In this article, we’ll look at how to do JavaScript string or integer comparisons.

How to do JavaScript string or integer comparisons?

To do JavaScript string or integer comparisons, we convert everything to integers before we compare them.

For instance, we write

parseInt("2") > parseInt("10")

to convert the strings to integers with parseInt before we compare them with >.

Conclusion

To do JavaScript string or integer comparisons, we convert everything to integers before we compare them.

Categories
JavaScript Answers

How to make fade out effect with pure JavaScript?

Sometimes, we want to make fade out effect with pure JavaScript.

In this article, we’ll look at how to make fade out effect with pure JavaScript.

How to make fade out effect with pure JavaScript?

To make fade out effect with pure JavaScript, we use CSS transitions.

For instance, we write

<p>Some text before</p>
<div id="target">Click to fade</div>
<p>Some text after</p>

to add some elements.

Then we write

#target {
  height: 100px;
  background-color: red;
  transition: opacity 1s;
}

to add a 1 second transition effect on the div with transition: opacity 1s;.

Then we write

const target = document.getElementById("target");

target.addEventListener("click", () => (target.style.opacity = "0"));
target.addEventListener("transitionend", () => target.remove());

to set the div’s opacity to 0.

After the transition is done, we remove the div with remove.

Conclusion

To make fade out effect with pure JavaScript, we use CSS transitions.