Categories
JavaScript Answers

How to convert hex to RGBA with JavaScript?

Sometimes, we want to convert hex to RGBA with JavaScript.

In this article, we’ll look at how to convert hex to RGBA with JavaScript.

How to convert hex to RGBA with JavaScript?

To convert hex to RGBA with JavaScript, we call the string match method.

For instance, we write

const hex2rgba = (hex, alpha = 1) => {
  const [r, g, b] = hex.match(/\w\w/g).map((x) => parseInt(x, 16));
  return `rgba(${r},${g},${b},${alpha})`;
};

to define the hex2rgba function.

In it, we call hex.match with a regex to match groups of 2 digits.

Then we call map on the returned array with a callback to parse the hex number with parseInt called with x and 16.

Finally, we return the string with the r, g, b and alpha values inside.

Conclusion

To convert hex to RGBA with JavaScript, we call the string match method.

Categories
JavaScript Answers

How to send a message to a particular client with socket.io and JavaScript?

Sometimes, we want to send a message to a particular client with socket.io and JavaScript.

In this article, we’ll look at how to send a message to a particular client with socket.io and JavaScript.

How to send a message to a particular client with socket.io and JavaScript?

To send a message to a particular client with socket.io and JavaScript, we call the socket.emit and socket.on method.

For instance, on client side, we write

const socket = io.connect("http://localhost");
socket.emit("join", { email: "user1@example.com" });

to call socket.emit to emit the join event with some event data.

Then on server side, we write

const io = require("socket.io").listen(80);

io.sockets.on("connection", (socket) => {
  socket.on("join", (data) => {
    socket.join(data.email);
  });
});

to call io.sockets.on with 'connection' to listen to the connection event.

In the callback, we call socket.on with 'join' to listen to the join event.

In the join callback, we get the email value we sent from data.email.

Conclusion

To send a message to a particular client with socket.io and JavaScript, we call the socket.emit and socket.on method.

Categories
JavaScript Answers

How to ignore user’s time zone and force Date() use specific time zone with JavaScript?

To ignore user’s time zone and force Date() use specific time zone with JavaScript, we can use the moment timezone library.

To install it, we run

npm install moment-timezone --save

Then we write

const moment = require("moment-timezone");
const d = moment(1270544790922)
  .tz("Europe/London")
  .format("YYYY-MM-DD HH:mm:ss");

to call moment with a timestamp in milliseconds.

And we set the time zone of the datetime with the tz method.

Then we call format to return a datetime string with the value returned by tz.

Categories
React Answers

How to compare objects with React useEffect hook?

Sometimes, we want to compare objects with React useEffect hook.

In this article, we’ll look at how to compare objects with React useEffect hook.

How to compare objects with React useEffect hook?

To compare objects with React useEffect hook, we can create a hook that keeps the previous value of a state.

For instance, we write

const usePrevious = (value) => {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
};

const useExample = (apiOptions) => {
  const myPreviousState = usePrevious(apiOptions);
  const [data, updateData] = useState([]);
  useEffect(() => {
    if (myPreviousState && !_.isEqual(myPreviousState, apiOptions)) {
      updateData(apiOptions);
    }
  }, [apiOptions]);
};

to create the usePrevious hook that keeps the value state’s value in a ref.

value is updated on every render since we call useEffect with no 2nd argument.

And then we return the ref’s value.

Then we use the usePrevious hook in the useExample hook.

In it, we call useEffect to run the callback when apiOptions changes.

We get the previous value of apiOptions with the usePrevious hook.

Then we check if the myPreviousState value is the same as apiOptions with the Lodash isEqual method.

Conclusion

To compare objects with React useEffect hook, we can create a hook that keeps the previous value of a state.

Categories
JavaScript Answers

How to add event handler with parameters with JavaScript?

Sometimes, we want to add event handler with parameters with JavaScript.

In this article, we’ll look at how to add event handler with parameters with JavaScript.

How to add event handler with parameters with JavaScript?

To add event handler with parameters with JavaScript, we can create a function that takes the parameters we want.

For instance, we write

const addClickHandler = (elem, arg1, arg2) => {
  elem.addEventListener(
    "click",
    (e) => {
      // ...
    },
    false
  );
};

to define the addClickHandler function that takes a few parameters.

In it, we call elem.addEventListener to add a click event listener to the elem element.

We can do whatever we want with arg1 and arg2 in the click listener callback.

Conclusion

To add event handler with parameters with JavaScript, we can create a function that takes the parameters we want.