Categories
JavaScript Answers

How to call class constructor without new keyword with JavaScript?

Sometimes, we want to call class constructor without new keyword with JavaScript.

In this article, we’ll look at how to call class constructor without new keyword with JavaScript.

How to call class constructor without new keyword with JavaScript?

To call class constructor without new keyword with JavaScript, we can define an arrow function and call it.

For instance, we write

const Foo = (x) => ({
  x,
  hello: () => `hello ${x}`,
  increment: () => Foo(x + 1),
  add: ({ x: y }) => Foo(x + y),
});

console.log(Foo(1).x);
console.log(Foo(1).hello());

to define the Foo arrow function that returns an object with the properties we want.

Then we call Foo to return a new object and access properties from it.

Conclusion

To call class constructor without new keyword with JavaScript, we can define an arrow function and call it.

Categories
React Answers

How to set multipart in Axios with React?

Sometimes, we want to set multipart in Axios with React.

In this article, we’ll look at how to set multipart in Axios with React.

How to set multipart in Axios with React?

To set multipart in Axios with React, we can submit a FormData object when making the request.

For instance, we write

const Comp = () => {
  const [file, setFile] = useState();

  const fileUpload = (file) => {
    const url = "http://example.com/file-upload";
    const formData = new FormData();
    formData.append("file", file);
    const config = {
      headers: {
        "content-type": "multipart/form-data",
      },
    };
    return post(url, formData, config);
  };

  const onFormSubmit = (e) => {
    e.preventDefault();
    const { data } = await fileUpload(file);
  };

  const onChange = (e) => {
    setFile(e.target.files[0]);
  };

  return (
    <form onSubmit={onFormSubmit}>
      <h1>File Upload</h1>
      <input type="file" onChange={onChange} />
      <button type="submit">Upload</button>
    </form>
  );
};

to create the Comp component.

In it, we define the file state with useState.

Then we define the fileUpload function that creates a FormData object.

Next, we add the file object into the FormData object with append.

And then we make the request with the Axios post method with the url, formData object and the config that has the headers that sets content-type header to multipart/form-data.

Next, we define the onFormSubmit function that calls preventDefauly to stop the default form submit behavior.

And then we call fileUpload to upload the file.

Finally, we render a form element with a file input in it.

When the file selection is changed, onChange is called.

And onFormSubmit is called when we click Upload.

Conclusion

To set multipart in Axios with React, we can submit a FormData object when making the request.

Categories
JavaScript Answers

How to disable arrow key scrolling in users browser with JavaScript?

Sometimes, we want to disable arrow key scrolling in users browser with JavaScript.

In this article, we’ll look at how to disable arrow key scrolling in users browser with JavaScript.

How to disable arrow key scrolling in users browser with JavaScript?

To disable arrow key scrolling in users browser with JavaScript, we disable the default behavior for arrow keys and the space key.

For instance, we write

window.addEventListener(
  "keydown",
  (e) => {
    if (
      ["Space", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].includes(
        e.code
      )
    ) {
      e.preventDefault();
    }
  },
  false
);

to call window.addEventListener to listen to the keydown event.

In the keydown event listener, we check if e.code is one of "Space", "ArrowUp", "ArrowDown", "ArrowLeft", or "ArrowRight".

If it is, then we call e.preventDefault to stop the default behavior.

Conclusion

To disable arrow key scrolling in users browser with JavaScript, we disable the default behavior for arrow keys and the space key.

Categories
JavaScript Answers

How to fix CKEditor instance already exists error with JavaScript?

Sometimes, we want to fix CKEditor instance already exists error with JavaScript.

In this article, we’ll look at how to fix CKEditor instance already exists error with JavaScript.

How to fix CKEditor instance already exists error with JavaScript?

To fix CKEditor instance already exists error with JavaScript, we destroy the existing instance if it already exists.

For instance, we write

const editor = CKEDITOR.instances[name];
if (editor) {
  editor.destroy(true);
}
CKEDITOR.replace(name);

to get the editor instance with CKEDITOR.instances[name].

If editor is defined, then we call editor.destroy with true to destroy the instance.

Then we call CKEDITOR.replace with name to create a new CKEditor instance with name.

Conclusion

To fix CKEditor instance already exists error with JavaScript, we destroy the existing instance if it already exists.

Categories
JavaScript Answers

How to test if value is a function with JavaScript?

Sometimes, we want to test if value is a function with JavaScript.

In this article, we’ll look at how to test if value is a function with JavaScript.

How to test if value is a function with JavaScript?

To test if value is a function with JavaScript, we can use the typeof operator.

For instance, we write

typeof document.getElementById("myform").onsubmit === "function";

to check if the document.getElementById("myform").onsubmit property is a function by checking if typeof document.getElementById("myform").onsubmit returns 'function'.

If it does, then it’s a function.

Conclusion

To test if value is a function with JavaScript, we can use the typeof operator.