Categories
React Answers

How to create dynamic href in React component?

Sometimes, we want to create dynamic href in React component

In this article, we’ll look at how to create dynamic href in React component.

How to create dynamic href in React component?

To create dynamic href in React component, we can use template strings.

For instance, we write

<a href={`/customer/${item._id}`}>
  {item.get("firstName")} {item.get("lastName")}
</a>;

in our component to set the href prop to the string returned by /customer/${item._id}.

Conclusion

To create dynamic href in React component, we can use template strings.

Categories
React Answers

How to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React?

Sometimes, we want to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React.

In this article, we’ll look at how to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React.

How to fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React?

To fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React, we shouldn’t call state setter functions at the top level of the component.

For instance, instead of writing

const SignInContainer = ({ message, variant }) => {
  const [open, setSnackBarState] = useState(false);
  const handleClose = (reason) => {
    if (reason === "clickaway") {
      return;
    }
    setSnackBarState(false);
  };

  if (variant) {
    setSnackBarState(true);
  }
  return (
    <div>
      <SnackBar
        open={open}
        handleClose={handleClose}
        variant={variant}
        message={message}
      />
      <SignInForm />
    </div>
  );
};

We write

const SignInContainer = ({ message, variant }) => {
  const [open, setSnackBarState] = useState(variant ? true : false);
  const handleClose = (reason) => {
    if (reason === "clickaway") {
      return;
    }
    setSnackBarState(false);
  };

  return (
    <div>
      <SnackBar
        open={open}
        handleClose={handleClose}
        variant={variant}
        message={message}
      />
      <SignInForm />
    </div>
  );
};

We removed the

if (variant) {
  setSnackBarState(true);
}

from the SignInContainer component so that we won’t keep re-rendering the component as we change the open state, which causes an infinite loop.

Conclusion

To fix Uncaught Invariant Violation: Too many re-renders. React limits the number of renders to prevent an infinite loop error with React, we shouldn’t call state setter functions at the top level of the component.

Categories
JavaScript Answers

How to generate unique random numbers between 1 and 100 with JavaScript?

Sometimes, we want to generate unique random numbers between 1 and 100 with JavaScript.

In this article, we’ll look at how to generate unique random numbers between 1 and 100 with JavaScript.

How to generate unique random numbers between 1 and 100 with JavaScript?

To generate unique random numbers between 1 and 100 with JavaScript, we can use the Math.random method.

For instance, we write

const arr = [];
while (arr.length < 10) {
  const r = Math.floor(Math.random() * 100) + 1;
  if (arr.indexOf(r) === -1) {
    arr.push(r);
  }
}
console.log(arr);

to use a while loop to put random numbers into the arr array until we have 10 random numbers.

We create the random number between 1 and 100 with

Math.floor(Math.random() * 100) + 1

Then we check if the number is already in arr with

arr.indexOf(r) === -1

If it isn’t, then we call push to push the number into the array.

Conclusion

To generate unique random numbers between 1 and 100 with JavaScript, we can use the Math.random method.

Categories
JavaScript Answers

How to remove all the children DOM elements in div with JavaScript?

Sometimes, we want to remove all the children DOM elements in div with JavaScript.

In this article, we’ll look at how to remove all the children DOM elements in div with JavaScript.

How to remove all the children DOM elements in div with JavaScript?

To remove all the children DOM elements in div with JavaScript, we can use the removeChild method.

For instance, we write

while (node.hasChildNodes()) {
  node.removeChild(node.lastChild);
}

to use a while loop to check if the node has any child nodes left with hasChildNodes.

Then we call remove.removeChild to remove the node‘s lastChild in the loop body.

Conclusion

To remove all the children DOM elements in div with JavaScript, we can use the removeChild method.

Categories
JavaScript Answers

How to call .map() a JavaScript ES6 Map with JavaScript?

Sometimes, we want to call .map() a JavaScript ES6 Map with JavaScript.

In this article, we’ll look at how to call .map() a JavaScript ES6 Map with JavaScript.

How to call .map() a JavaScript ES6 Map with JavaScript?

To call .map() a JavaScript ES6 Map with JavaScript, we convert it to an array, and then back to a map.

For instance, we write

const myMap = new Map([
  ["thing1", 1],
  ["thing2", 2],
  ["thing3", 3],
]);
const newEntries = Array.from(myMap, ([key, value]) => [key, value + 1]);
const newMap = new Map(newEntries);

to call Array.from with myMap and a callback to map the values to a new values for the map.

Then we create the map from the newEntries array.

Conclusiin

To call .map() a JavaScript ES6 Map with JavaScript, we convert it to an array, and then back to a map.