Categories
JavaScript Answers

How to render an array.map() in React?

Sometimes, we want to render an array.map() in React.

In this article, we’ll look at how to render an array.map() in React.

How to render an array.map() in React?

To render an array.map() in React, we call map with a function that returns the elements we want.

For instance, we write

data.map((item, key) => {
  return (
    <tr key={key}>
      <td>{item.heading}</td>
      <td>{item.date}</td>
      <td>{item.status}</td>
    </tr>
  );
});

to call data.map with a function that returns tr elements.

We set the key prop to a unique value so that React can identify the component being rendered.

item is the entry in data being looped through.

Conclusion

To render an array.map() in React, we call map with a function that returns the elements we want.

Categories
JavaScript Answers

How to use JavaScript fetch to make DELETE and PUT requests?

Sometimes, we want to use JavaScript fetch to make DELETE and PUT requests.

In this article, we’ll look at how to use JavaScript fetch to make DELETE and PUT requests.

How to use JavaScript fetch to make DELETE and PUT requests?

To use JavaScript fetch to make DELETE and PUT requests, we can call fetch with the method option.

For instance, we write

const createNewProfile = async (profile) => {
  const formData = new FormData();
  formData.append("first_name", profile.firstName);
  formData.append("last_name", profile.lastName);
  formData.append("email", profile.email);

  const response = await fetch("http://example.com/api/v1/registration", {
    method: "POST",
    body: formData,
  });
  return response.json();
};

const json = await createNewProfile(profile);

to call fetch with an object that has the method property set to a string with the request method.

We set body to the request body.

And then we return the response JSON promise by returning response.json.

Conclusion

To use JavaScript fetch to make DELETE and PUT requests, we can call fetch with the method option.

Categories
JavaScript Answers

How to go from one page to another page using JavaScript?

Sometimes, we want to go from one page to another page using JavaScript.

In this article, we’ll look at how to go from one page to another page using JavaScript.

How to go from one page to another page using JavaScript?

To go from one page to another page using JavaScript, we set the window.location.href property to the string with the URL we want to navigate to.

For instance, we write

window.location.href = "sample.html";

to set window.location.href to "sample.html" to navigate to sample.html.

Conclusion

To go from one page to another page using JavaScript, we set the window.location.href property to the string with the URL we want to navigate to.

Categories
JavaScript Answers

How to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript?

Sometimes, we want to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript.

In this article, we’ll look at how to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript.

How to fix the fetch Missing boundary in multipart/form-data POST error with JavaScript?

To fix the fetch Missing boundary in multipart/form-data POST error with JavaScript, we set the Accept request header to "*/*" to accept all response MIME types.

For instance, we write

const options = {
  //...
  headers: new HttpHeaders({
    Accept: "*/*",
    Authorization:
      "Bearer " + JSON.parse(sessionStorage.getItem("token")).token,
    "Access-Control-Allow-Origin": this.apiURL,
    "Access-Control-Allow-Methods": "GET, POST, OPTIONS, PUT, PATCH, DELETE",
    "Access-Control-Allow-Headers":
      "origin,X-Requested-With,content-type,accept",
    "Access-Control-Allow-Credentials": "true",
  }),
  //...
};
await fetch(url, options);

to create a HttpHeaders object with the Accept header set to "*/*" to set "*/*" as the Accept request header value.

Then we call fetch with the options object to make a request to the url with all the request headers listed.

Conclusion

To fix the fetch Missing boundary in multipart/form-data POST error with JavaScript, we set the Accept request header to "*/*" to accept all response MIME types.

Categories
JavaScript Answers

How to detect click outside div using JavaScript?

Sometimes, we want to detect click outside div using JavaScript.

In this article, we’ll look at how to detect click outside div using JavaScript.

How to detect click outside div using JavaScript?

To detect click outside div using JavaScript, we can check if e.target doesn’t equal the inner element.

For instance, we write

document.getElementById("outer-container").onclick = (e) => {
  if (e.target !== document.getElementById("content-area")) {
    console.log("You clicked outside");
  } else {
    console.log("You clicked inside");
  }
};

to check that e.target doesn’t equal to document.getElementById("content-area").

If it’s not, then we clicked outside of the element with ID content-area.

We add a click listener to the element with ID outer-container by setting onclick to the click listener function.

Conclusion

To detect click outside div using JavaScript, we can check if e.target doesn’t equal the inner element.