Categories
JavaScript Answers

How to display an image stored as byte array in HTML and JavaScript?

Sometimes, we want to display an image stored as byte array in HTML and JavaScript.

In this article, we’ll look at how to display an image stored as byte array in HTML and JavaScript.

How to display an image stored as byte array in HTML and JavaScript?

To display an image stored as byte array in HTML and JavaScript, we can convert the byte array to a base64 URL string.

For instance, we add an img element with

<img id="itemPreview" src="" />

Then we write

const blob = new Blob([uint8ArrayBuffer], { type: "image/jpeg" });
const imageUrl = URL.createObjectURL(blob);
document.getElementById("itemPreview").src = imageUrl;

to select the img element with getElementById.

Then we set the src property to the base64 URL with the byte array converted to a base64 string.

We get the base64 image URL from by creating the uint8ArrayBuffer to a blob with the Blob constructor.

Then we call URL.createObjectURL to convert the blob to a base64 string.

Conclusion

To display an image stored as byte array in HTML and JavaScript, we can convert the byte array to a base64 URL string.

Categories
JavaScript Answers

How to automatically open default email client and pre-populate content with JavaScript?

Sometimes, we want to automatically open default email client and pre-populate content with JavaScript.

In this article, we’ll look at how to automatically open default email client and pre-populate content with JavaScript.

How to automatically open default email client and pre-populate content with JavaScript?

To automatically open default email client and pre-populate content with JavaScript, we can navigate to the mailto URL with the email subject and body filled in.

For instance, we write

const email = message.emailId;
const subject = message.subject;
const emailBody = "Hi " + message.from;
document.location =
  "mailto:" + email + "?subject=" + subject + "&body=" + emailBody;

to set document.location to a mailto URL string with the subject and body of the email.

This will make the browser open the default mail client with the subject and body of the email filled in with the email address.

Conclusion

To automatically open default email client and pre-populate content with JavaScript, we can navigate to the mailto URL with the email subject and body filled in.

Categories
React Answers

How to update and merge state object using React useState hook?

Sometimes, we want to update and merge state object using React useState hook.

In this article, we’ll look at how to update and merge state object using React useState hook.

How to update and merge state object using React useState hook?

To update and merge state object using React useState hook, we can call the state setter function with a callback that returns the new object state value.

For instance, we write

const Comp = () => {
  const [state, setState] = useState({ fName: "", lName: "" });
  const handleChange = (e) => {
    const { name, value } = e.target;
    setState((prevState) => ({
      ...prevState,
      [name]: value,
    }));
  };

  return (
    <>
      <input
        value={state.fName}
        type="text"
        onChange={handleChange}
        name="fName"
      />
      <input
        value={state.lName}
        type="text"
        onChange={handleChange}
        name="lName"
      />
    </>
  );
};

to define the handleChange in the Comp component that calls setState with a callback that return an object with the prevState properties and the new property values.

We set the onChange prop to handleChange to get the input value from e.target.value and the name attribute value of the input from e.target.name.

Conclusion

To update and merge state object using React useState hook, we can call the state setter function with a callback that returns the new object state value.

Categories
JavaScript Answers

How to save session only cookies with JavaScript?

Sometimes, we want to save session only cookies with JavaScript.

In this article, we’ll look at how to save session only cookies with JavaScript.

How to save session only cookies with JavaScript?

To save session only cookies with JavaScript, we can use session storage instead of cookies.

For instance, we write

const myVariable = { a: [1, 2, 3, 4], b: "some text" };

sessionStorage["myVariable"] = JSON.stringify(myVariable);
const readValue = JSON.parse(sessionStorage["myVariable"]);

to save an entry with key myVariable into session storage with

sessionStorage["myVariable"] = JSON.stringify(myVariable);

Since myVariable is an object, we’ve convert it to a string with JSON.stringify before we can save the value.

Then we read the value by the key with

sessionStorage["myVariable"]

Then we call JSON.parse to parse the stringified JSON object into a JavaScript object.

Conclusion

To save session only cookies with JavaScript, we can use session storage instead of cookies.

Categories
JavaScript Answers

How to convert relative path to absolute with Node.js?

Sometimes, we want to convert relative path to absolute with Node.js.

In this article, we’ll look at how to convert relative path to absolute with Node.js.

How to convert relative path to absolute with Node.js?

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.

For instance, we write

const { resolve } = require("path");

const absPath = resolve("../../bb/tmp.txt");

to call resolve with a relative path to return a string with the equivalent absolute path.

Conclusion

To convert relative path to absolute with Node.js, we can use the path module’s resolve function.