Categories
React Answers

How to add react meta description with i18n-next React?

To add react meta description with i18n-next React, we can use the useIntl hook with React Helmet.

We install React Helmet with

npm i react-helmet

Then we use the useIntl hook to get the formatMessage function.

And then we add the meta tag into the Helmet component provided by React Helmet.

Then we set the content prop to a string returned by the f function to get the translated string by the id property.

import { useIntl } from 'react-intl';

const DescriptionMeta = () => {
  const { formatMessage: f } = useIntl();

  return (
    <Helmet>
      <meta name="description" content={f({ id: 'meta.description' })} />
    </Helmet>
  );
};
Categories
React Answers

How to turn off suspense with i18n-next React?

To turn off suspense with i18n-next React, we set the useSuspense option to false.

To do this, we write

i18n
  .use(XHR)
  .use(LanguageDetector)
  .init({
    react: { 
      useSuspense: false 
    }
});

to set useSuspense to false in the object we pass into the init method.

Categories
React Answers

How to add a image and video lightbox with React?

To add a image and video lightbox with React, we use the React image & video lightbox component.

To install it, we run

npm i react-image-video-lightbox

Then we use it buy writing

<ReactImageVideoLightbox
  data={[
    {
      url: "https://placekitten.com/450/300",
      type: "photo",
      altTag: "some image",
    },
    {
      url: "https://www.youtube.com/embed/ScMzIvxBSi4",
      type: "video",
      title: "some video",
    },
    {
      url: "https://placekitten.com/550/500",
      type: "photo",
      altTag: "some other image",
    },
    {
      url: "https://www.youtube.com/embed/ScMzIvxBSi4",
      type: "video",
      title: "some other video",
    },
  ]}
  startIndex={0}
  showResourceCount={true}
  onCloseCallback={callbackFunction}
  onNavigationCallback={(currentIndex) =>
    console.log(`Current index: ${currentIndex}`)
  }
/>

We add all the content for our lightbox into the data prop of the ReactImageVideoLightbox component by setting data to an array of items we want to show.

Then they’ll all show on the screen.

Categories
React Answers

How to make the image background full width with React?

To make the image background full width with React, we set the backgroundSize CSS style to cover.

For instance, we write

import TechnicalImage from "./images/technical.jpg";

const divStyle = {
  width: "88%",
  height: "800px",
  backgroundImage: `url(${TechnicalImage})`,
  backgroundSize: "cover",
};

<div style={divStyle}>...</div>;

to set the backgroundSize to cover.

Then the image background full width.

Categories
JavaScript Answers

How to Check the HTML Element’s CSS display Property Value with JavaScript?

Sometimes, we want to check the HTML element’s CSS display property value with JavaScript.

In this article, we’ll look at how to check the HTML element’s CSS display property value with JavaScript.

Check the HTML Element’s CSS display Property Value with JavaScript

To check the HTML element’s CSS display property value with JavaScript, we can use the window.getComputedStyle method with the element we want to get the display property for.

We can also use the style.display property if the display CSS property value isn’t inherited from another location.

For instance, if we have the following HTML:

<div style='display: inline'>
  hello world
</div>

Then we can get the display property by writing:

const element = document.querySelector('div')
const {
  display
} = window.getComputedStyle(element, null);
console.log(display)

console.log(element.style.display);

First, we get the div with document.querySelector .

We call getComputedStyle with the element to get an object with the styles of element .

Then we get the display property from the returned object.

Also, we use the element.style.display property to get the display CSS property value.

Therefore, both console logs should log 'inline' .

Conclusion

To check the HTML element’s CSS display property value with JavaScript, we can use the window.getComputedStyle method with the element we want to get the display property for.

We can also use the style.display property if the display CSS property value isn’t inherited from another location.