Categories
React Answers

How to fix onClick doesn’t render new React component?

Spread the love

Sometimes, we want to fix onClick doesn’t render new React component.

In this article, we’ll look at how to fix onClick doesn’t render new React component.

How to fix onClick doesn’t render new React component?

To fix onClick doesn’t render new React component, we can use React Router.

For instance, we write

import { withRouter } from "react-router-dom";
import Button from "components/button";

const yourComponent = ({ history }) => {
  return (
    <Button onClick={() => history.push("/path")}> New componentPage </Button>
  );
};

export default withRouter(yourComponent);

to create the yourComponent component that calls history.push with the destination path to navigate to it when we click on the button.

The history prop is available since we call withRouter with yourComponent.

Conclusion

To fix onClick doesn’t render new React component, we can use React Router.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *