Categories
JavaScript Answers

How to use JavaScript switch with logical operators?

Sometimes, we want to use JavaScript switch with logical operators.

In this article, we’ll look at how to use JavaScript switch with logical operators.

How to use JavaScript switch with logical operators?

To use JavaScript switch with logical operators, we use true as the expression to compare.

For instance, we write

switch (true) {
  case count >= 4:
    console.log("lol");
    break;
  case count > 3:
    console.log("bye");
    break;
  case count == 2:
    console.log("hi");
    break;
}

to use true in the parentheses and then we can use any boolean expression we want for the case statements.

Conclusion

To use JavaScript switch with logical operators, we use true as the expression to compare.

Categories
TypeScript Answers

How to fix Can’t find TypeScript compiler: Command “tsc” is not valid with TypeScript?

Sometimes, we want to fix Can’t find TypeScript compiler: Command "tsc" is not valid with TypeScript.

In this article, we’ll look at how to fix Can’t find TypeScript compiler: Command "tsc" is not valid with TypeScript.

How to fix Can’t find TypeScript compiler: Command "tsc" is not valid with TypeScript?

To fix Can’t find TypeScript compiler: Command "tsc" is not valid with TypeScript, we install the typescript package.

To install it, we run

npm install -g typescript

to install the package globally.

Then we can run tsc anywhere.

Conclusion

To fix Can’t find TypeScript compiler: Command "tsc" is not valid with TypeScript, we install the typescript package.

Categories
JavaScript Answers

How to fix Uncaught TypeError: Cannot read property ‘push’ of undefined with React-Router-Dom?

Sometimes, we want to fix Uncaught TypeError: Cannot read property ‘push’ of undefined with React-Router-Dom.

In this article, we’ll look at how to fix Uncaught TypeError: Cannot read property ‘push’ of undefined with React-Router-Dom.

How to fix Uncaught TypeError: Cannot read property ‘push’ of undefined with React-Router-Dom?

To fix Uncaught TypeError: Cannot read property ‘push’ of undefined with React-Router-Dom, we call the useHistory hook.

For instance, we write

import { useHistory } from "react-router-dom";

to import the useHistory hook.

Then we write

const history = useHistory();

to get the history object from the hook.

Then we use

history.push("/foo");

in a component function to navigate to /foo.

Conclusion

To fix Uncaught TypeError: Cannot read property ‘push’ of undefined with React-Router-Dom, we call the useHistory hook.

Categories
JavaScript Answers

How to get image preview before uploading in React?

Sometimes, we want to get image preview before uploading in React.

In this article, we’ll look at how to get image preview before uploading in React.

How to get image preview before uploading in React?

To get image preview before uploading in React, we read the selected file into a base64 URL string.

For instance, we write

const App = () => {
  const [selectedImage, setSelectedImage] = useState();

  const imageChange = (e) => {
    if (e.target.files && e.target.files.length > 0) {
      setSelectedImage(e.target.files[0]);
    }
  };

  const removeSelectedImage = () => {
    setSelectedImage();
  };

  return (
    <>
      <div>
        <input accept="image/*" type="file" onChange={imageChange} />

        {selectedImage && (
          <div>
            <img src={URL.createObjectURL(selectedImage)} alt="Thumb" />
            <button onClick={removeSelectedImage}>Remove This Image</button>
          </div>
        )}
      </div>
    </>
  );
};

to add a file input and an img element.

We set the file input’s onChange prop to the imageChange function.

In it, we get the selected file e.target.files[0] and set that as the value of the selectedImage state.

And then we convert that to a base64 URL with URL.createObjectURL.

Conclusion

To get image preview before uploading in React, we read the selected file into a base64 URL string.

Categories
JavaScript Answers

How to convert date and time to Unix timestamp with JavaScript?

Sometimes, we want to convert date and time to Unix timestamp with JavaScript.

In this article, we’ll look at how to convert date and time to Unix timestamp with JavaScript.

How to convert date and time to Unix timestamp with JavaScript?

To convert date and time to Unix timestamp with JavaScript, we use the getTime method.

For instance, we write

const unixtime = Date.parse("24-Nov-2022 17:57:35").getTime() / 1000;

to parse the datetime string with Date.parse.

Then we call getTime to return the Unix timestamp in milliseconds.

And then we divide that by 1000 to get the equivalent in seconds.

Conclusion

To convert date and time to Unix timestamp with JavaScript, we use the getTime method.