Categories
TypeScript Answers

How to fix Error: *.default is not a constructor with TypeScript?

Sometimes, we want to fix Error: *.default is not a constructor with TypeScript.

In this article, we’ll look at how to fix Error: *.default is not a constructor with TypeScript.

How to fix Error: *.default is not a constructor with TypeScript?

To fix Error: *.default is not a constructor with TypeScript, we should make sure we export a class as the default export.

For instance, we write

map_action_file.ts

export default class MapAction implements IMapAction {
  //...
}

to export the MapAction class in map_action_file.ts as a default export.

Then we import it by writing

import MapAction from "./map_action_file";

in another file in the same folder.

Conclusion

To fix Error: *.default is not a constructor with TypeScript, we should make sure we export a class as the default export.

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
CSS

How to make HTML text input field grow while typing with CSS?

Sometimes, we want to make HTML text input field grow while typing with CSS.

In this article, we’ll look at how to make HTML text input field grow while typing with CSS.

How to make HTML text input field grow while typing with CSS?

To make HTML text input field grow while typing with CSS, we set the max-width style of a contenteditable element.

For instance, we write

span {
  border: solid 1px black;
}
div {
  max-width: 200px;
}

to set the max width of the container div to 200px.

Then we write

<div>
  <span contenteditable="true">sdfsd</span>
</div>

to add a contenteditable span inside the div so we can type in the span.

Conclusion

To make HTML text input field grow while typing with CSS, we set the max-width style of a contenteditable element.