Categories
React Answers

How to clean up memory leaks on an unmounted component in React Hooks?

Sometimes, we want to clean up memory leaks on an unmounted component in React Hooks.

In this article, we’ll look at how to clean up memory leaks on an unmounted component in React Hooks.

How to clean up memory leaks on an unmounted component in React Hooks?

To clean up memory leaks on an unmounted component in React Hooks, we use the useMountedState hook from the react-use package.

To install it, we run

npm i react-use

Then in our component, we add

const isMounted = useMountedState();

useEffect(() => {
  const asyncAction = executeAsyncAction();

  asyncAction.then((result) => {
    if (isMounted) {
      // ...
    }
  });
}, []);

to call the useMountedState hook to return a boolean to indicate if the component is mounted.

And then we check isMounted in the useEffect callback before we do anything.

Conclusion

To clean up memory leaks on an unmounted component in React Hooks, we use the useMountedState hook from the react-use package.

Categories
React Answers

How to pass props with styled-components with React?

To pass props with styled-components with React, we pass it in as its child.

For instance, we write

const StyledWrapper = styled.div`
  /* ... */
`;

const Wrapper = ({ message }) => {
  return <StyledWrapper>{message}</StyledWrapper>;
};

to create the StyledWrapper component from the styled-components styled.div tag.

Then we create the Wrapper component that takes the message prop and pass it in as its child.

Categories
React Answers

How to add TypeScript interface signature for the onClick event in React?

Sometimes, we want to add TypeScript interface signature for the onClick event in React.

In this article, we’ll look at how to add TypeScript interface signature for the onClick event in React.

How to add TypeScript interface signature for the onClick event in React?

To add TypeScript interface signature for the onClick event in React, we use the React.MouseEventHandler<HTMLButtonElement> type.

For instance, we write

interface IPropsSquare {
  message: string;
  onClick: React.MouseEventHandler<HTMLButtonElement>;
}

to define the IPropsSquare interface.

In it, we add the onClick property and set it to the React.MouseEventHandler<HTMLButtonElement> type.

Then we can set the onClick prop to a function that’s used as the click event handler for the button without type errors.

Conclusion

To add TypeScript interface signature for the onClick event in React, we use the React.MouseEventHandler<HTMLButtonElement> type.

Categories
React Answers

How to fix Expected onClick listener to be a function, instead got type string error with React?

Sometimes, we want to fix Expected onClick listener to be a function, instead got type string error with React.

In this article, we’ll look at how to fix Expected onClick listener to be a function, instead got type string error with React.

How to fix Expected onClick listener to be a function, instead got type string error with React?

To fix Expected onClick listener to be a function, instead got type string error with React, we should make sure we set the onClick prop to a function reference.

For instance, we write

const activateLasers = () => {
  //...
};

<button onClick={activateLasers}>Activate Lasers</button>;

to set the onClick prop to the activateLasers function.

We don’t call the function.

We just set the function reference as the onClick prop’s value.

Conclusion

To fix Expected onClick listener to be a function, instead got type string error with React, we should make sure we set the onClick prop to a function reference.

Categories
React Answers

How to display images in React using JSX without import?

Sometimes, we want to display images in React using JSX without import.

In this article, we’ll look at how to display images in React using JSX without import.

How to display images in React using JSX without import?

To display images in React using JSX without import, we use require.

For instance, we write

const imageName = require("./images/image1.jpg");

//...

const Comp = () => {
  //...
  return <img src={imageName} />;
};

to call require with the image path to import the image path.

Then we can use that as the value of the src property to load the image.

Conclusion

To display images in React using JSX without import, we use require.