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
JavaScript Answers

How to fix Charts.js graph not scaling to canvas size with JavaScript?

Sometimes, we want to fix Charts.js graph not scaling to canvas size with JavaScript.

In this article, we’ll look at how to fix Charts.js graph not scaling to canvas size with JavaScript.

How to fix Charts.js graph not scaling to canvas size with JavaScript?

To fix Charts.js graph not scaling to canvas size with JavaScript, we set the responsive option to false.

For instance, we write

const ctx = document.getElementById("myChart").getContext("2d");
const myChart = new Chart(ctx, {
  type: "line",
  data: {
    labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    datasets: [
      {
        label: "My First dataset",
        data: [1, 2, 3, 2, 1, 2, 3, 4, 5, 4],
      },
    ],
  },
  options: {
    responsive: false,
  },
});

to set options.responsive to false to make the graph scale to the canvas size.

We get the canvas with getElementById and get its context with getContext.

Conclusion

To fix Charts.js graph not scaling to canvas size with JavaScript, we set the responsive option to false.

Categories
JavaScript Answers

How to auto refresh page every 30 seconds with JavaScript?

Sometimes, we want to auto refresh page every 30 seconds with JavaScript.

In this article, we’ll look at how to auto refresh page every 30 seconds with JavaScript.

How to auto refresh page every 30 seconds with JavaScript?

To auto refresh page every 30 seconds with JavaScript, we call setTimeout with a callback that calls location.reload.

For instance, we write

window.setTimeout(() => {
  window.location.reload();
}, 30000);

to call setTimeout with a callback that calls location.reload after 30000 milliseconds or 30 seconds.

Conclusion

To auto refresh page every 30 seconds with JavaScript, we call setTimeout with a callback that calls location.reload.

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
JavaScript Answers

How to dynamically add variable name value pairs to JSON Object with JavaScript?

Sometimes, we want to dynamically add variable name value pairs to JSON Object with JavaScript.

In this article, we’ll look at how to dynamically add variable name value pairs to JSON Object with JavaScript.

How to dynamically add variable name value pairs to JSON Object with JavaScript?

To dynamically add variable name value pairs to JSON Object with JavaScript, we use square brackets.

For instance, we write

const obj = {};
obj["name"] = value;
obj["anotherName"] = anotherValue;

to define the obj object.

Then we add the name and anotherName properties into it.

This is the same as

const obj = { name: value, anotherName: anotherValue };

We can use keys that aren’t valid JavaScript property names with square brackets.

Conclusion

To dynamically add variable name value pairs to JSON Object with JavaScript, we use square brackets.