Categories
JavaScript Answers

How to disable a Link if it’s active with React Router?

Sometimes, we want to disable a Link if it’s active with React Router.

In this article, we’ll look at how to disable a Link if it’s active with React Router.

How to disable a Link if it’s active with React Router?

To disable a Link if it’s active with React Router, we set the to prop.

For instance, we write

<Link to={isActive ? "/link-to-route" : "#"} />;

to set the to prop to "#" if isActive is true.

Otherwise, we set it to "/link-to-route".

Conclusion

To disable a Link if it’s active with React Router, we set the to prop.

Categories
JavaScript Answers

How to use ternary operator in JSX to include HTML with React?

Sometimes, we want to use ternary operator in JSX to include HTML with React.

In this article, we’ll look at how to use ternary operator in JSX to include HTML with React.

How to use ternary operator in JSX to include HTML with React?

To use ternary operator in JSX to include HTML with React, we use JSX code as expressions for the ternary operator’s operands.

For instance, we write

const Comp = () => {
  //...

  return (
    <div className="row">
      {message === "failed" ? (
        <div> Something went wrong </div>
      ) : (
        <div> Everything is fine </div>
      )}
    </div>
  );
};

to if message is 'failed'.

If it is, then we render <div> Something went wrong </div>.

Otherwise, we render <div> Everything is fine </div>.

Conclusion

To use ternary operator in JSX to include HTML with React, we use JSX code as expressions for the ternary operator’s operands.

Categories
JavaScript Answers

How to get the exact local time of client with JavaScript?

Sometimes, we want to get the exact local time of client with JavaScript.

In this article, we’ll look at how to get the exact local time of client with JavaScript.

How to get the exact local time of client with JavaScript?

To get the exact local time of client with JavaScript, we use the Intl.DateTimeFormat constructor.

For instance, we write

const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

to call resolvedOptions on the Intl.DateTimeFormat object to return an object with the client’s datetime options.

We get the time zone of the device with the timeZone property.

Conclusion

To get the exact local time of client with JavaScript, we use the Intl.DateTimeFormat constructor.

Categories
JavaScript Answers

How to create a form dynamically via JavaScript?

Sometimes, we want to create a form dynamically via JavaScript.

In this article, we’ll look at how to create a form dynamically via JavaScript.

How to create a form dynamically via JavaScript?

To create a form dynamically via JavaScript, we use the createElement method.

For instance, we write

const f = document.createElement("form");
f.setAttribute("method", "post");
f.setAttribute("action", "submit.php");

const i = document.createElement("input");
i.setAttribute("type", "text");
i.setAttribute("name", "username");

const s = document.createElement("input");
s.setAttribute("type", "submit");
s.setAttribute("value", "Submit");

f.appendChild(i);
f.appendChild(s);

document.body.appendChild(f);

to call createElement with 'form' to create a form element.

Then we set its attributes by calling setAttribute with the attribute name and value.

Next, we call createElement with 'input' to create input elements.

Then we call f.appendChild to append the inputs to the form as its children.

Finally, we call document.body.appendChild with f to append the form as the body’s last child.

Conclusion

To create a form dynamically via JavaScript, we use the createElement method.

Categories
JavaScript Answers

How to fix HTML2Canvas only rendering what is visible on screen with JavaScript?

Sometimes, we want to fix HTML2Canvas only rendering what is visible on screen with JavaScript.

In this article, we’ll look at how to fix HTML2Canvas only rendering what is visible on screen with JavaScript.

How to fix HTML2Canvas only rendering what is visible on screen with JavaScript?

To fix HTML2Canvas only rendering what is visible on screen with JavaScript, we set the scrollY value.

For instance, we write

const canvas = await html2canvas(htmlSource, { scrollY: -window.scrollY });
const img = canvas.toDataURL();
window.open(img);

to call html2canvas with an object that sets scrollY to -window.scrollY so that we position htmlSource is moved so that the whole htmlSource element will be rendered on the canvas.

Then we call canvas.toDataURL to return the base64 string with the rendered element’s image.

Conclusion

To fix HTML2Canvas only rendering what is visible on screen with JavaScript, we set the scrollY value.