Categories
React Answers

How to return multiple lines JSX in another return statement in React?

Sometimes, we want to return multiple lines JSX in another return statement in React.

In this article, we’ll look at how to return multiple lines JSX in another return statement in React.

How to return multiple lines JSX in another return statement in React?

To return multiple lines JSX in another return statement in React, we can wrap our components with fragments.

For instance, we write

const Comp = () => {
  return [1, 2, 3].map((n, index) => {
    return (
      <React.Fragment key={index}>
        <h3>Item {n}</h3>
        <p>Description {n}</p>
      </React.Fragment>
    );
  });
};

to wrap the h3 and p elements in the React.Fragement component in the map callback.

Then these elements will be rendered without a wrapper element around them.

Conclusion

To return multiple lines JSX in another return statement in React, we can wrap our components with fragments.

Categories
React Answers

How to define more than one style name using CSS modules with React?

Sometimes, we want to define more than one style name using CSS modules with React.

In this article, we’ll look at how to define more than one style name using CSS modules with React.

How to define more than one style name using CSS modules with React?

To define more than one style name using CSS modules with React, we can put them class names in one string.

For instance, we write

function Footer(props) {
  return (
    <div className={styles.footer}>
      <div className={`${styles.description} ${styles.yellow}`}>
        <p>this site was created by me</p>
      </div>
    </div>
  );
}

to set the className prop of the inner prop to

`${styles.description} ${styles.yellow}`

to apply multiple classes imported from the CSS module.

Conclusion

To define more than one style name using CSS modules with React, we can put them class names in one string.

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
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.

Categories
React Answers

How to compare objects with React useEffect hook?

Sometimes, we want to compare objects with React useEffect hook.

In this article, we’ll look at how to compare objects with React useEffect hook.

How to compare objects with React useEffect hook?

To compare objects with React useEffect hook, we can create a hook that keeps the previous value of a state.

For instance, we write

const usePrevious = (value) => {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
};

const useExample = (apiOptions) => {
  const myPreviousState = usePrevious(apiOptions);
  const [data, updateData] = useState([]);
  useEffect(() => {
    if (myPreviousState && !_.isEqual(myPreviousState, apiOptions)) {
      updateData(apiOptions);
    }
  }, [apiOptions]);
};

to create the usePrevious hook that keeps the value state’s value in a ref.

value is updated on every render since we call useEffect with no 2nd argument.

And then we return the ref’s value.

Then we use the usePrevious hook in the useExample hook.

In it, we call useEffect to run the callback when apiOptions changes.

We get the previous value of apiOptions with the usePrevious hook.

Then we check if the myPreviousState value is the same as apiOptions with the Lodash isEqual method.

Conclusion

To compare objects with React useEffect hook, we can create a hook that keeps the previous value of a state.