Categories
React Answers

How to add multiple classes to a ReactJS Component?

To add multiple classes to a ReactJS Component, we can set the className propt to a string with the classes separated by spaces.

For instance, we write

<li className={[activeClass, data.klass, "main-class"].join(" ")}>...</li>;

to join the activeClass, data.klass and 'main-class strings together with join.

Then all the classes are applied to the li element.

Categories
React Answers

How to programmatically navigate using React Router and JavaScript?

To programmatically navigate using React Router and JavaScript, we can use the useHistory hook.

For instance, we write

import { useHistory } from "react-router-dom";

const HomeButton = () => {
  const history = useHistory();

  const handleClick = () => {
    history.push("/home");
  };

  return (
    <button type="button" onClick={handleClick}>
      Go home
    </button>
  );
};

to call the useHistory hook to return the history object.

Then we define the handleClick function that calls history.push to navigate to the /home page.

And then we set the onClick prop toi handleClick to call handleClick when we click oin the button.

Categories
React Answers

How to add loop with React JSX?

To add loop with React JSX, we use the array map method.

For instance, we write

<div>
  {[...Array(10)].map((x, i) => (
    <ObjectRow key={i} />
  ))}
</div>

to render the array’s data by creating array with 10 entries with Array(10).

Then we call map with a callback that returns the ObjectRow component to render its contents.

We set the key prop to i to set a unique key for each item.

Categories
React Answers

How to pass props to this.props.children with React?

To pass props to this.props.children with React, we call the React.cloneElement method.

For instance, we write

<div>
  {React.cloneElement(this.props.children[0], {
    loggedIn: true,
    testPropB: true,
  })}
  {React.cloneElement(this.props.children[1], {
    loggedIn: true,
    testPropA: false,
  })}
</div>;

to call cloneElement with the child element to render and an object with thge props that we want to pass into the child component.

We pass the loggedIn and testPropB props to the this.props.children[0] component.

And we pass the loggedIn and testPropA props to the this.props.children[1] component.

Categories
React Answers

How to fix React-router URLs don’t work when refreshing or writing manually?

To fix React-router URLs don’t work when refreshing or writing manually, we should fix our server config.

For instance, we write

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html [L]
</IfModule>

in the .htaccess file if we’re using Apache.

The file redirects all requests to index.html of the React app so that React Router can be used to render the routes instead of the server.