Categories
React Native Answers

How to require image module using dynamic names with React Native?

Sometimes, we want to require image module using dynamic names with React Native.

In this article, we’ll look at how to require image module using dynamic names with React Native.

How to require image module using dynamic names with React Native?

To require image module using dynamic names with React Native, we can call require with a string based on another value.

For instance, we write

const icon = props.active
  ? require("image!my-icon-active")
  : require("image!my-icon-inactive");
<Image source={icon} />;

to call require with different image names based on the value of props.active.

And then we set the source prop to icon in the Image component.

Conclusion

To require image module using dynamic names with React Native, we can call require with a string based on another value.

Categories
JavaScript Answers

How to fix webpack –watch isn’t compiling changed files with JavaScript?

Sometimes, we want to fix webpack –watch isn’t compiling changed files with JavaScript.

In this article, we’ll look at how to fix webpack –watch isn’t compiling changed files with JavaScript.

How to fix webpack –watch isn’t compiling changed files with JavaScript?

To fix webpack –watch isn’t compiling changed files with JavaScript, we can increase the number of watchers and ignore files that don’t have to be watched.

We run

echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

to increase the number of watchers to 524288.

Also, in our Webpack config, we write

//...

module.exports = {
  //...
  watchOptions: {
    poll: true,
    ignored: /node_modules/,
  },
  //...
};

to ignore the /node_modules/ folder in the watch options so that Webpack doesn’t watch anything in the folder for changes.

Conclusion

To fix webpack –watch isn’t compiling changed files with JavaScript, we can increase the number of watchers and ignore files that don’t have to be watched.

Categories
React Answers

How to redirect page on click of a button with React Router v6?

Sometimes, we want to redirect page on click of a button with React Router v6.

In this article, we’ll look at how to redirect page on click of a button with React Router v6.

How to redirect page on click of a button with React Router v6?

To redirect page on click of a button with React Router v6, we use the useNavigate hook.

For instance, we write

import React from "react";
import { useNavigate } from "react-router-dom";

function LoginLayout() {
  let navigate = useNavigate();
  const routeChange = () => {
    let path = `newPath`;
    navigate(path);
  };

  return (
    <div className="app flex-row align-items-center">
      <Container>
        <Button color="primary" className="px-4" onClick={routeChange}>
          Login
        </Button>
      </Container>
    </div>
  );
}

to call the useNavigate hook to return the navigate function.

Then we call navigate with path to navigate to the path in the routeChange function.

Then we assign routeChange as the value of the onClick prop of the Button to navigate to the path on click.

Conclusion

To redirect page on click of a button with React Router v6, we use the useNavigate hook.

Categories
JavaScript Answers

How to get a microtime in Node.js?

Sometimes, we want to get a microtime in Node.js.

In this article, we’ll look at how to get a microtime in Node.js.

How to get a microtime in Node.js?

To get a microtime in Node.js, we can use the process.hrtime method.

For instance, we write

const [seconds, nanoSeconds] = process.hrtime();
console.log(seconds * 1000000 + nanoSeconds / 1000);

to call process.hrtime to return an array with the current timestamp as an array with the seconds and nanoSeconds.

Then we use seconds * 1000000 + nanoSeconds / 1000 to convert seconds and nanoSeconds to microseconds and add them together.

Conclusion

To get a microtime in Node.js, we can use the process.hrtime method.

Categories
React Answers

How to display line breaks from saved text area with React?

Sometimes, we want to display line breaks from saved text area with React.

In this article, we’ll look at how to display line breaks from saved text area with React.

How to display line breaks from saved text area with React?

To display line breaks from saved text area with React, we set the white-space CSS style to pre-line.

For instance, we write

const Comp = () => {
  //...
  return (
    <>
      <style>{`
      #p-wrap {
        white-space: pre-line;
      }
    `}</style>
      <textarea value={address} />
      <p id="p-standard">{address}</p>
      <hr />
      <p id="p-wrap">{address}</p>
    </>
  );
};

to add the textarea and p elements.

We set the element with ID p-wrap to have the white-space style set to pre-line.

Then this p element will display all the line breaks that are entered into the textarea.

Conclusion

To display line breaks from saved text area with React, we set the white-space CSS style to pre-line.