Categories
React Answers

How to use saved image with React?

To use saved image with React, we can call require with the image path to and set the returned path as the value of the src prop of the img element.

For instance, we write

<img alt="timer" src={require('./images/timer.png')} />

to add an img element with the src prop set to the path returned by calling require with the local image path.

Categories
React Answers

How to add an SVG with React?

To add an SVG with React, we can import it as a component and add it directly into our components.

For instance, we write

import React from "react";
import { ReactComponent as BrandIcon } from "./assets/brand-icon.svg";

export default function () {
  return (
    <div>
      <BrandIcon />
    </div>
  );
}

to import the BrandIcon SVG and put it into our component to render it.

Categories
React Answers

How to use two text fields in one single row with React Material UI?

<Grid container spacing={24}>
  <Grid item xs={4}>
    <TextField
      label="PS"
      value={ps}
      onChange={handleChange}
      margin="normal"
      type="number"
      margin="dense"
      variant="filled"
    />
  </Grid>
  <Grid item xs={4}>
    <TextField
      label="MOOE"
      value={mode}
      onChange={handleChange}
      margin="normal"
      type="number"
      margin="dense"
      variant="filled"
    />
  </Grid>
  <Grid item xs={4}>
    <TextField
      label="CO"
      value={co}
      onChange={handleChange}
      margin="normal"
      type="number"
      margin="dense"
      variant="filled"
    />
  </Grid>
</Grid>;

to add our TextFields into Grid components.

We set the width relative to the screen’s width with xs to set the width to 4 out of 12 of the screen width for all screen sizes.

The inner Grids has the item prop to make them an item inside the Grid with the container prop.

Categories
React Native Answers

How to hide the navigation navbar with React Native?

To hide the navigation navbar with React Native, we set the screenOptions.headerShown option to false.

For instance, we write

<Stack.Navigator
  screenOptions={{
    headerShown: false,
  }}
>
  ...
</Stack.Navigator>;

to set the headerShown option of the screenOptions prop object to false on the Stack.Navigator to hide the navbar.

Categories
JavaScript Answers

How to Get the Time Difference Between Two Dates in Seconds with JavaScript?

Sometimes, we want to get the time difference between 2 dates in seconds with JavaScript.

In this article, we’ll look at how to get the time difference between 2 dates in seconds with JavaScript.

Convert the Dates to Timestamps in Milliseconds Then Get the Difference Between Them and Convert the Difference to Seconds

We can convert the dates to timestamps in milliseconds.

Then we can subtract them to get the difference between them.

And then we can convert the difference in milliseconds to seconds.

For instance, we can write:

const startDate = new Date(2020, 1, 1);  
const endDate = new Date(2020, 2, 1);  
const seconds = (endDate.getTime() - startDate.getTime()) / 1000;  
console.log(seconds)

We have the startDate and endDate which we can convert both to timestamps in milliseconds with getTime .

Then we subtract the timestamp of endDate by the timestamp of startDate .

This will return the timestamp difference in milliseconds.

So we divide the difference by 1000 to get the difference in seconds.

Therefore, seconds should be 2505600 .

We can replace getTime with the unary + operator.

For instance, we can write:

const startDate = new Date(2020, 1, 1);  
const endDate = new Date(2020, 2, 1);  
const seconds = (+endDate - +startDate) / 1000;  
console.log(seconds)

to convert startDate and endDate to timestamps with the unary + operator.

And we get the same result as before.

Conclusion

We can get the time difference between 2 dates in seconds by converting them both to timestamps.

Then we can subtract the timestamps and convert the difference to seconds.