Categories
React Answers

How to add CSS display: none within conditional with React JSX?

Sometimes, we want to add CSS display: none within conditional with React JSX.

In this article, we’ll look at how to add CSS display: none within conditional with React JSX.

How to add CSS display: none within conditional with React JSX?

To add CSS display: none within conditional with React JSX, we set the style prop.

For instance, we write

<div style={{ display: showStore ? "block" : "none" }}>
  <div>a lot more divs</div>
</div>

to set the style prop to an object that has the display property set to 'block' if showStore is true and 'none' otherwise.

Conclusion

To add CSS display: none within conditional with React JSX, we set the style prop.

Categories
Vue Answers

How to use local storage with Vue.js?

Sometimes, we want to use local storage with Vue.js.

In this article, we’ll look at how to use local storage with Vue.js.

How to use local storage with Vue.js?

To use local storage with Vue.js, we call the localStorage methods.

For instance, we write

localStorage.setItem("item", response.data);

to call localStorage.setItem with the key and value of the item.

We get the item by the key by writing

localStorage.getItem("item");

We call getItem with the key of the item.

And we remove an item from local storage by writing

localStorage.removeItem("item");

to call removeItem to remove the item with the key.

Conclusion

To use local storage with Vue.js, we call the localStorage methods.

Categories
JavaScript Answers

How to extract parameter value from URL using regular expressions and JavaScript?

Sometimes, we want to extract parameter value from URL using regular expressions and JavaScript.

In this article, we’ll look at how to extract parameter value from URL using regular expressions and JavaScript.

How to extract parameter value from URL using regular expressions and JavaScript?

To extract parameter value from URL using regular expressions and JavaScript, we use the regex match method.

For instance, we write

const regex = /http\:\/\/www\.youtube\.com\/watch\?v=([\w-]{11})/;
const url = "http://www.youtube.com/watch?v=Ahg6qcgoay4";
const [, id] = url.match(regex);

to call url.match with the regex that has a capture group ([\w-]{11}) to get the value of the v query parameter.

Then we call url.match with regex to return an array with the video’s id as the 2nd value of the array.

Conclusion

To extract parameter value from URL using regular expressions and JavaScript, we use the regex match method.

Categories
JavaScript Answers

How to compose multipart/form-data with a different Content-Type on each part with JavaScript?

Sometimes, we want to compose multipart/form-data with a different Content-Type on each part with JavaScript.

In this article, we’ll look at how to compose multipart/form-data with a different Content-Type on each part with JavaScript.

How to compose multipart/form-data with a different Content-Type on each part with JavaScript?

To compose multipart/form-data with a different Content-Type on each part with JavaScript, we call the form data append method with a blob.

For instance, we write

const formData = new FormData();

formData.append(
  "items",
  new Blob(
    [
      JSON.stringify({
        name: "Book",
        quantity: "12",
      }),
    ],
    {
      type: "application/json",
    }
  )
);

to call formData.append with the key and a blob.

In the blob, we have the JSON content as a string in an array.

And we set the MIME type of the content by setting type to "application/json".

Conclusion

To compose multipart/form-data with a different Content-Type on each part with JavaScript, we call the form data append method with a blob.

Categories
JavaScript Answers

How to hide the address bar on iPhone with JavaScript?

Sometimes, we want to hide the address bar on iPhone with JavaScript.

In this article, we’ll look at how to hide the address bar on iPhone with JavaScript.

How to hide the address bar on iPhone with JavaScript?

To hide the address bar on iPhone with JavaScript, we scroll 1 pixel down.

For instance, we write

window.scrollTo(0, 1);

to call scrollTo to scroll the browser tab or window 1 pixel down.

Conclusion

To hide the address bar on iPhone with JavaScript, we scroll 1 pixel down.