Categories
JavaScript Answers

How to make a non-cached request with fetch and JavaScript?

Sometimes, we want to make a non-cached request with fetch and JavaScript.

In this article, we’ll look at how to make a non-cached request with fetch and JavaScript.

How to make a non-cached request with fetch and JavaScript?

To make a non-cached request with fetch and JavaScript, we can set the pragma and cache-control request headers.

For instance, we write

const myImage = document.querySelector("img");

const headers = new Headers();
myHeaders.append("pragma", "no-cache");
myHeaders.append("cache-control", "no-cache");

const myInit = {
  method: "GET",
  headers,
};

const myRequest = new Request("myImage.jpg");

const response = await fetch(myRequest, myInit);
const blob = await response.blob();
const objectURL = URL.createObjectURL(blob);
myImage.src = objectURL;

to call fetch with myRequest and myInit.

In myInit, we set the headers to the headers object.

We set the pragma header to 'no-cache' and cache-control to 'no-cache with append to make a non-cached request.

Then we get the blob from the response with blob.

Conclusion

To make a non-cached request with fetch and JavaScript, we can set the pragma and cache-control request headers.

Categories
JavaScript Answers

How to listen for scroll event for iPhone or iPad with JavaScript?

Sometimes, we want to listen for scroll event for iPhone or iPad with JavaScript.

In this article, we’ll look at how to listen for scroll event for iPhone or iPad with JavaScript.

How to listen for scroll event for iPhone or iPad with JavaScript?

To listen for scroll event for iPhone or iPad with JavaScript, we can listen to the scroll event.

For instance, we write

window.addEventListener("scroll", () => {
  console.log("Scrolled");
});

window.onscroll = () => {
  console.log("Scrolled");
};

to call window.addEventListener to add the scroll event listener.

The callback runs when we scroll on our iPhone or iPad.

Likewise, we set window.onscroll to a function that runs when we scroll to add a scroll event listener.

Conclusion

To listen for scroll event for iPhone or iPad with JavaScript, we can listen to the scroll event.

Categories
React Answers

How to replace img src onerror with React?

Sometimes, we want to replace img src onerror with React.

In this article, we’ll look at how to replace img src onerror with React.

How to replace img src onerror with React?

To replace img src onerror with React, we set the fallback image URL when there’s an error loading the image.

For instance, we write

import React, { ImgHTMLAttributes, useState } from "react";

export default function ImageWithFallback({ fallback, src, ...props }) {
  const [imgSrc, setImgSrc] = useState(src);
  const onError = () => setImgSrc(fallback);

  return <img src={imgSrc ? imgSrc : fallback} onError={onError} {...props} />;
}

to create the imgSrc state.

We set imgSrc to src initially.

Then we add the img element with an onError prop that’s set to the onError function.

In onError, we call setImgSrc to set the image URL to fallback.

And then set the src prop of the img element to imgSrc or fallback if imgSrc isn’t set.

Conclusion

To replace img src onerror with React, we set the fallback image URL when there’s an error loading the image.

Categories
JavaScript Answers

How to move element in the DOM in JavaScript?

Sometimes, we want to move element in the DOM in JavaScript.

In this article, we’ll look at how to move element in the DOM in JavaScript.

How to move element in the DOM in JavaScript?

To move element in the DOM in JavaScript, we can use the before and after methods.

For instance, we write

const div1 = document.getElementById("div1");
const div2 = document.getElementById("div2");
const div3 = document.getElementById("div3");

div2.after(div1);
div2.before(div3);

to select 3 divs with getElementById.

Then we call div2.after with div1 to insert div1 after div2.

And we call div2.before with div3 to insert div3 before div2.

Conclusion

To move element in the DOM in JavaScript, we can use the before and after methods.

Categories
React Native Answers

How to fix ‘React.Children.only expected to receive a single React element child’ error when putting Image and TouchableHighlight in a View with React Native?

Sometimes, we want to fix ‘React.Children.only expected to receive a single React element child’ error when putting Image and TouchableHighlight in a View with React Native.

In this article, we’ll look at how to fix ‘React.Children.only expected to receive a single React element child’ error when putting Image and TouchableHighlight in a View with React Native.

How to fix ‘React.Children.only expected to receive a single React element child’ error when putting Image and TouchableHighlight in a View with React Native?

To fix ‘React.Children.only expected to receive a single React element child’ error when putting Image and TouchableHighlight in a View with React Native, we should have a single child component in the TouchableHighlight component.

For instance, we write

const Comnp = () => {
  //...
  const { height, width } = Dimensions.get("window");
  return (
    <View style={styles.container}>
      <Image
        style={{
          height,
          width,
        }}
        source={require("image!foo")}
        resizeMode="cover"
      />
      <TouchableHighlight style={styles.button}>
        <Text> highlight text</Text>
      </TouchableHighlight>
    </View>
  );
};

to add the Text component into the TouchableHighlight component.

We keep the Image outside the TouchableHighlight so we only have a single child component in the TouchableHighlight component.

Conclusion

To fix ‘React.Children.only expected to receive a single React element child’ error when putting Image and TouchableHighlight in a View with React Native, we should have a single child component in the TouchableHighlight component.