Categories
React Answers

How to run code after React hooks state variable is updated?

Sometimes, we want to run code after React hooks state variable is updated.

In this article, we’ll look at how to run code after React hooks state variable is updated.

How to run code after React hooks state variable is updated?

To run code after React hooks state variable is updated, we can watch the state’s value with the useEffect callback and run code when the value we’re looking for is set.

For instance, we write:

import React, { useEffect, useState } from "react";

export default function App() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    if (count === 5) {
      console.log("you win");
    }
  }, [count]);

  return (
    <div>
      <button onClick={() => setCount((c) => c + 1)}>increment</button>
      <p>{count}</p>
    </div>
  );
}

We define the count state with the useState hook.

Then we add a button that calls setCount to increment the count when we click on it.

Then we add the useEffect callback that watches count‘s value and log 'you win' when count is 5.

Conclusion

To run code after React hooks state variable is updated, we can watch the state’s value with the useEffect callback and run code when the value we’re looking for is set.

Categories
Python Answers

How to set the background color for react-select drop downs?

Sometimes, we want to set the background color for react-select drop downs.

In this article, we’ll look at how to set the background color for react-select drop downs.

How to set the background color for react-select drop downs?

To set the background color for react-select drop downs, we can return an object with the color values set.

For instance, we write:

import React from "react";
import Select from "react-select";

const customStyles = {
  control: (base, state) => ({
    ...base,
    background: "#023950",
    borderRadius: state.isFocused ? "3px 3px 0 0" : 3,
    borderColor: state.isFocused ? "yellow" : "green",
    boxShadow: state.isFocused ? null : null,
    "&:hover": {
      borderColor: state.isFocused ? "red" : "blue"
    }
  }),
  menu: (base) => ({
    ...base,
    borderRadius: 0,
    marginTop: 0
  }),
  menuList: (base) => ({
    ...base,
    padding: 0
  })
};

const options = [
  { label: "Apple", value: "apple" },
  { label: "Orange", value: "orange" }
];

export default function App() {
  return (
    <form>
      <Select styles={customStyles} options={options} />
    </form>
  );
}

We set the styles prop to the customStyles object which has various styles.

The control method in the object returns an object with the style values.

The properties returned includes background, borderRadius, borderColor, boxShadow and other CSS style properties.

We can also style states like hover with "&:hover".

And we can get the state of the drop down from the state parameter.

Likewise, we have the menu and menuList methods to style the menu.

We set the options prop to an array of options.

Now we see we the drop down has a dark blue background and the drop down row that’s hovered over has a light blue background.

Conclusion

To set the background color for react-select drop downs, we can return an object with the color values set.

Categories
React Answers

How to fix the input type file onChange not firing issue with React?

Sometimes, we want to fix the input type file onChange not firing issue with React.

In this article, we’ll look at how to fix the input type file onChange not firing issue with React.

How to fix the input type file onChange not firing issue with React?

To fix the input type file onChange not firing issue with React, we set the onChange prop to a function that takes the change event object.

For instance, we write:

import React from "react";

export default function App() {
  const onChange = (e) => {
    console.log(e.target.files);
  };

  return (
    <form>
      <input type="file" multiple onChange={onChange} />
    </form>
  );
}

We have a file input which we create by setting the type attribute to file.

And we add the multiple prop to it to make it allow multiple file selection.

Finally, we set the onChange prop to the onChange function to get the files selected when they’re selected.

Therefore, when we select one or more files with the file input, we should see the e.target.files logged.

e.target.files should have all the files selected.

Conclusion

To fix the input type file onChange not firing issue with React, we set the onChange prop to a function that takes the change event object.

Categories
React Answers

How to enable multiple file selection with the file input with React?

Sometimes, we want to enable multiple file selection with the file input with React

In this article, we’ll look at how to enable multiple file selection with the file input with React

How to input multiple file from form with React?

To enable multiple file selection with the file input with React, we can add the multiple prop to the file input.

For instance, we write:

import React from "react";

export default function App() {
  const onChange = (e) => {
    console.log(e.target.files);
  };

  return (
    <form>
      <input type="file" multiple onChange={onChange} />
    </form>
  );
}

We have a file input which we create by setting the type attribute to file.

And we add the multiple prop to it to make it allow multiple file selection.

Finally, we set the onChange prop to the onChange function to get the files selected when they’re selected.

Therefore, when we select one or more files with the file input, we should see the e.target.files logged.

e.target.files should have all the files selected.

Conclusion

To enable multiple file selection with the file input with React, we can add the multiple prop to the file input.

Categories
React Answers

How to add a delay onMouseOver event handler with React?

Sometimes, we want to add a delay onMouseOver event handler with React.

In this article, we’ll look at how to add a delay onMouseOver event handler with React.

How to add a delay onMouseOver event handler with React?

To add a delay onMouseOver event handler with React, we can use the Lodash debounce function.

For instance, we write:

import React, { useState } from "react";
import { debounce } from "lodash";

export default function App() {
  const [isHovered, setIsHovered] = useState(false);
  console.log(isHovered);

  const debouncedHandleMouseEnter = debounce(() => setIsHovered(true), 500);

  const handlOnMouseLeave = () => {
    setIsHovered(false);
    debouncedHandleMouseEnter.cancel();
  };

  return (
    <div
      onMouseEnter={debouncedHandleMouseEnter}
      onMouseLeave={handlOnMouseLeave}
    >
      hover me
    </div>
  );
}

We defined the isHovered state with useState.

Then we define the debouncedHandleMouseEnter function by passing in the event handler function into Lodash’s debounce function.

We set the debounce delay to 500ms.

Next, we have the handlOnMouseLeave function where we call debouncedHandleMouseEnter.cancel to cancel the calling of the debouncedHandleMouseEnter function when handlOnMouseLeave is called.

Therefore, when we hover over the div, we see isHovered becomes true.

And when we move our mouse away from the div, isHovered becomes false.

Conclusion

To add a delay onMouseOver event handler with React, we can use the Lodash debounce function.