Categories
JavaScript Answers

How to pass this element to JavaScript onclick function and add a class to that clicked element?

Sometimes, we want to pass this element to JavaScript onclick function and add a class to that clicked element.

In this article, we’ll look at how to pass this element to JavaScript onclick function and add a class to that clicked element.

How to pass this element to JavaScript onclick function and add a class to that clicked element?

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that’s called with this.

For instance, we write

<div class="row" style="padding-left: 21px">
  <ul class="nav nav-tabs" style="padding-left: 40px">
    <li class="active filter">
      <a href="#month" onclick="onClick(this)">This Month</a>
    </li>
    <li class="filter"><a href="#year" onclick="onClick(this)">Year</a></li>
    <li class="filter">
      <a href="#last60" onclick="onClick(this)">60 Days</a>
    </li>
    <li class="filter">
      <a href="#last90" onclick="onClick(this)">90 Days</a>
    </li>
  </ul>
</div>

to set the onclick attribute to the a elements to call onClick with this.

Then we define the onClick function by writing

function onClick(element) {
  element.removeClass("active");
  element.addClass("active");
}

in our JavaScript code.

We call element.removeClass to remove the 'active' class and we call addClass to add the 'active' class.

Conclusion

To pass this element to JavaScript onclick function and add a class to that clicked element, we can set the onclick attribute to a function that’s called with this.

Categories
JavaScript Answers

How to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript?

Sometimes, we want to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript.

In this article, we’ll look at how to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript.

How to fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript?

To fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript, we set the style prop to an object with the styles.

For instance, we write

<span className="myClass" style={{ float: "left", paddingRight: "5px" }}>
  foobar
</span>

to set the style prop to an object with some style values.

Conclusion

To fix React inline style – style prop expects a mapping from style properties to values, not a string error with JavaScript, we set the style prop to an object with the styles.

Categories
JavaScript Answers

How to fix async function in mocha before() is always finished before it() spec with JavaScript?

Sometimes, we want to fix async function in mocha before() is always finished before it() spec with JavaScript.

In this article, we’ll look at how to fix async function in mocha before() is always finished before it() spec with JavaScript.

How to fix async function in mocha before() is always finished before it() spec with JavaScript?

To fix async function in mocha before() is always finished before it() spec with JavaScript, we can return a promise in the before callback.

For instance, we write

let a = 0;

before(() => {
  return new Promise((resolve) => {
    setTimeout(() => {
      a = 1;
      resolve();
    }, 200);
  });
});

it("a should be set to 1", () => {
  assert(a === 1);
});

to call before with a callback that returns a promise.

Then the it callback should run after the promise in the before callback is finished.

Conclusion

To fix async function in mocha before() is always finished before it() spec with JavaScript, we can return a promise in the before callback.

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.