Categories
React Native Answers

How to close React Native modal by clicking on overlay with JavaScript?

Sometimes, we want to close React Native modal by clicking on overlay with JavaScript.

In this article, we’ll look at how to close React Native modal by clicking on overlay with JavaScript.

How to close React Native modal by clicking on overlay with JavaScript?

To close React Native modal by clicking on overlay with JavaScript, we set the visible prop.

For instrance, we write

import React, { useState } from "react";
import {
  Button,
  StyleSheet,
  View,
  TouchableOpacity,
  Modal,
} from "react-native";

const App = () => {
  const [show, setShow] = useState(false);
  return (
    <View>
      <TouchableOpacity style={{ marginTop: 200 }}>
        <Button title="show" onPress={() => setShow(!show)} />
      </TouchableOpacity>
      <Modal transparent={true} visible={show} animationType="slide">
        <TouchableOpacity
          activeOpacity={1}
          style={{ backgroundColor: "#000000aa", flex: 1 }}
          onPress={() => setShow(!show)}
        />

        <View style={{ backgroundColor: "#FFFFFF", flex: 1 }}>
          <View>
            <Button title="close" onPress={() => setShow(!show)} />
          </View>
        </View>
      </Modal>
    </View>
  );
};
export default App;

to set the visible prop to the show state.

If show is true, then the Modal is shown.

Otherwise, it’s hidden.

We call setShow to show the modal in the TouchableOpacity components.

Conclusion

To close React Native modal by clicking on overlay with JavaScript, we set the visible prop.

Categories
React Native Answers

How to animate the rotation of an Image and React Native and JavaScript?

Sometimes, we want to animate the rotation of an Image and React Native and JavaScript.

In this article, we’ll look at how to animate the rotation of an Image and React Native and JavaScript.

How to animate the rotation of an Image and React Native and JavaScript?

To animate the rotation of an Image and React Native and JavaScript, we use the Animated component.

For instance, we write

import { Animated } from "react-native";

//...

<Animated.View style={{ transform: [{ rotate: "10 deg" }] }}>
  <YourComponent />
</Animated.View>;

to wrap Animated.View around YourComponent animate YourComponent .

We set the style prop to { transform: [{ rotate: "10 deg" }] } to rotate the View 10 degrees.

Conclusion

To animate the rotation of an Image and React Native and JavaScript, we use the Animated component.

Categories
React Native Answers

How to overlay ActivityIndicator in React Native?

Sometimes, we want to overlay ActivityIndicator in React Native.

In this article, we’ll look at how to overlay ActivityIndicator in React Native.

How to overlay ActivityIndicator in React Native?

To overlay ActivityIndicator in React Native, we can put it in a modal.

For instance, we write

<Modal
  transparent={true}
  animationType={"none"}
  visible={loading}
  onRequestClose={() => {
    console.log("close modal");
  }}
>
  <View style={styles.modalBackground}>
    <View style={styles.activityIndicatorWrapper}>
      <ActivityIndicator animating={loading} />
    </View>
  </View>
</Modal>

to put the ActivityIndicator in a Modal.

We have transparent set to true to make the modal transparent so we can see the contents underneath it.

And we set the animating to the loading state to make it animate only when loading is true.

Also, we set visible to loading to show the modal only when loading is true.

Conclusion

To overlay ActivityIndicator in React Native, we can put it in a modal.

Categories
React Answers

How to display binary data as image in React?

Sometimes, we want to display binary data as image in React.

In this article, we’ll look at how to display binary data as image in React.

How to display binary data as image in React?

To display binary data as image in React, we can set the src prop of the img element to a base64 URL.

For instance, we write

const data =
  "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

const Example = ({ data }) => <img src={`data:image/jpeg;base64,${data}`} />;

to set the src prop of the img element in Example to a base64 URL with the binary image data.

Then the image will be displayed.

Conclusion

To display binary data as image in React, we can set the src prop of the img element to a base64 URL.

Categories
React Answers

How to only allow numbers in a number input in React?

Sometimes, we want to only allow numbers in a number input in React.

In this article, we’ll look at how to only allow numbers in a number input in React.

How to only allow numbers in a number input in React?

To only allow numbers in a number input in React, we can check for the key that’s pressed.

For instance, we write

<input
  onKeyPress={(event) => {
    if (!/[0-9]/.test(event.key)) {
      event.preventDefault();
    }
  }}
/>

to set the onKeyPress prop to a function that checks the key that’s pressed with event.key.

If it’s not 0 to 9, then we call preventDefault to stop the character from being appending to the input value.

Conclusion

To only allow numbers in a number input in React, we can check for the key that’s pressed.