Categories
JavaScript Answers

How to fill the whole canvas with specific color with JavaScript?

Sometimes, we want to fill the whole canvas with specific color with JavaScript.

In this article, we’ll look at how to fill the whole canvas with specific color with JavaScript.

How to fill the whole canvas with specific color with JavaScript?

To fill the whole canvas with specific color with JavaScript, we can use the canvas context fillRect method.

For instance, we write

<canvas width="300" height="150" id="canvas"> </canvas>

to add a canvas.

Then we write

const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);

to select the canvas with getElementById.

And the we call getContext to get the context.

Next, we set the background color by setting the fillStyle.

And then we call fillRect with 0, 0, canvas.width, canvas.height to draw rectangle that fills the whole canvas with the fillStyle color.

Conclusion

To fill the whole canvas with specific color with JavaScript, we can use the canvas context fillRect method.

Categories
React Answers

How to import JSON file in React?

Sometimes, we want to import JSON file in React.

In this article, we’ll look at how to import JSON file in React.

How to import JSON file in React?

To import JSON file in React, we use import to import it like a regular module.

For instance, we write

import Profile from "./components/profile";

to import the ./components/profile JSON file as Profile.

This works because the json-loader Webpack module is included with create-react-app.

Conclusion

To import JSON file in React, we use import to import it like a regular module.

Categories
JavaScript Answers

How to determine if Chrome is in incognito mode via a script with JavaScript?

Sometimes, we want to determine if Chrome is in incognito mode via a script with JavaScript.

In this article, we’ll look at how to determine if Chrome is in incognito mode via a script with JavaScript.

How to determine if Chrome is in incognito mode via a script with JavaScript?

To determine if Chrome is in incognito mode via a script with JavaScript, we check if the file systems API can’t be used.

For instance, we write

const fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) {
  console.log("check failed");
} else {
  fs(
    window.TEMPORARY,
    100,
    console.log.bind(console, "not in incognito mode"),
    console.log.bind(console, "incognito mode")
  );
}

to check if fs is undefined.

If it’s undefined, then the script is run in incognito mode.

Otherwise, we call fs with callbacks to check if it’s executed successfully.

The 3rd argument is the success callback and the 4th is the error callback.

If the error callback is run, then the user is in incognito mode.

Conclusion

To determine if Chrome is in incognito mode via a script with JavaScript, we check if the file systems API can’t be used.

Categories
React Answers

How to create dynamic href in React component?

Sometimes, we want to create dynamic href in React component

In this article, we’ll look at how to create dynamic href in React component.

How to create dynamic href in React component?

To create dynamic href in React component, we can use template strings.

For instance, we write

<a href={`/customer/${item._id}`}>
  {item.get("firstName")} {item.get("lastName")}
</a>;

in our component to set the href prop to the string returned by /customer/${item._id}.

Conclusion

To create dynamic href in React component, we can use template strings.

Categories
React Answers

How to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React?

Sometimes, we want to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React.

In this article, we’ll look at how to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React.

How to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React?

To fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React, we shouldn’t call state setter functions at the top level of the component.

For instance, instead of writing

const SignInContainer = ({ message, variant }) => {
  const [open, setSnackBarState] = useState(false);
  const handleClose = (reason) => {
    if (reason === "clickaway") {
      return;
    }
    setSnackBarState(false);
  };

  if (variant) {
    setSnackBarState(true);
  }
  return (
    <div>
      <SnackBar
        open={open}
        handleClose={handleClose}
        variant={variant}
        message={message}
      />
      <SignInForm />
    </div>
  );
};

We write

const SignInContainer = ({ message, variant }) => {
  const [open, setSnackBarState] = useState(variant ? true : false);
  const handleClose = (reason) => {
    if (reason === "clickaway") {
      return;
    }
    setSnackBarState(false);
  };

  return (
    <div>
      <SnackBar
        open={open}
        handleClose={handleClose}
        variant={variant}
        message={message}
      />
      <SignInForm />
    </div>
  );
};

We removed the

if (variant) {
  setSnackBarState(true);
}

from the SignInContainer component so that we won’t keep re-rendering the component as we change the open state, which causes an infinite loop.

Conclusion

To fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React, we shouldn’t call state setter functions at the top level of the component.