Categories
React Answers

How to call multiple functions on click with React and JavaScript?

Sometimes, we want to call multiple functions on click with React and JavaScript.

In this article, we’ll look at how to call multiple functions on click with React and JavaScript.

How to call multiple functions on click with React and JavaScript?

To call multiple functions on click with React and JavaScript, we create a function that calls all the functions and use that as the click event handler.

For instance, we write

const App = () => {
  const fun1 = () => {
    console.log("hello");
  };

  const fun2 = () => {
    console.log("world");
  };

  return (
    <div>
      <button
        onClick={() => {
          fun1();
          fun2();
        }}
      >
        Click
      </button>
    </div>
  );
};

to set the onClick prop of the button to a function that calls fun1 and func2 when we click on the button.

Conclusion

To call multiple functions on click with React and JavaScript, we create a function that calls all the functions and use that as the click event handler.

Categories
JavaScript Answers

How to zip two arrays in JavaScript?

Sometimes, we want to zip two arrays in JavaScript.

In this article, we’ll look at how to zip two arrays in JavaScript.

How to zip two arrays in JavaScript?

To zip two arrays in JavaScript, we call the array map method.

For instance, we write

const zip = (a, b) => a.map((k, i) => [k, b[i]]);

console.log(zip([1, 2, 3], ["a", "b", "c"]));

to create the zip function that calls a.map with a callback that returns an array with the entry k in a and entry b[i] in b.

And then we call zip with 2 equal sized arrays to zip them together.

The returned array will have arrays that have the values in [1, 2, 3] and ["a", "b", "c"] at the same index.

Conclusion

To zip two arrays in JavaScript, we call the array map method.

Categories
JavaScript Answers

How to loop through a JSON array with JavaScript?

Sometimes, we want to loop through a JSON array with JavaScript.

In this article, we’ll look at how to loop through a JSON array with JavaScript.

How to loop through a JSON array with JavaScript?

To loop through a JSON array with JavaScript, we can use a for of loop.

For instance, we write

const json = [
  {
    id: "1",
    msg: "hi",
    tid: "2022-05-05 23:35",
    fromWho: "hello1@email.cpm",
  },
  {
    id: "2",
    msg: "there",
    tid: "2022-05-05 23:45",
    fromWho: "hello2@email.cpm",
  },
];

for (const obj of json) {
  console.log(obj.id);
}

to loop through the json array with a for of loop.

We assign the entry being looped through to obj.

Then we get the value of the id property of the object in the loop and log it.

Conclusion

To loop through a JSON array with JavaScript, we can use a for of loop.

Categories
JavaScript Answers

How to remove last comma from a string with JavaScript?

Sometimes, we want to remove last comma from a string with JavaScript.

In this article, we’ll look at how to remove last comma from a string with JavaScript.

How to remove last comma from a string with JavaScript?

To remove last comma from a string with JavaScript, we can use the string replace method.

For instance, we write

const newStr = str.replace(/,\s*$/, "");

to call str.replace with a regex that matches the comma at the end of the string or before the whitespaces that’s at the end of the string.

And we call it with an empty string to replace the commas with an empty string.

Conclusion

To remove last comma from a string with JavaScript, we can use the string replace method.

Categories
JavaScript Answers

How to set request timeout with Fetch API?

Sometimes, we want to set request timeout with Fetch API.

In this article, we’ll look at how to set request timeout with Fetch API.

How to set request timeout with Fetch API?

To set request timeout with Fetch API, we can use the AbortController constructor.

For instance, we write

const controller = new AbortController();

const timeoutId = setTimeout(() => controller.abort(), 5000);

const req = async () => {
  const response = await fetch(url, { signal: controller.signal });
  //...
  clearTimeout(timeoutId);
};
req();

to create an AbortController object.

Then we call abort in the setTimeout function to cancel the request that’s been assigned the abort controller signal object after 5 seconds.

Then we call fetch with the url to make the request to and the signal property of the object in the 2nd argument is set to controller.signal.

Assigning signal to controller.signal lets us abort the request with controller.abort.

After the request is done, we call clearTimeout with the timeoutId to cancel the setTimeout timer.

If the timer is cancelled after 5 seconds, then abort runs and the request will be cancelled.

Conclusion

To set request timeout with Fetch API, we can use the AbortController constructor.