-2

the error

TypeError: Cannot read properties of undefined (reading 'push')

My code

import React from "react";

import { useHistory } from "react-router-dom";
const test = () => {
  const history = useHistory();

  function buttonPressed() {
    history.push("/sus");
  }
  return (
    <div>
      <button onClick={() => buttonPressed()}>Brn</button>
    </div>
  );
};

export default test ;

PLEASE HELP ME

  • Does this answer your question? [Cannot read property 'history' of undefined (useHistory hook of React Router 5)](https://stackoverflow.com/questions/58220995/cannot-read-property-history-of-undefined-usehistory-hook-of-react-router-5) – Nimna Perera Oct 14 '21 at 02:12
  • 1
    *All React component names must start with a capital letter* – Shohin Oct 14 '21 at 02:40

1 Answers1

1

Make sure your router is configured properly to handle a push to route "/sus".

By the way, you can just call the onClick handler with

<button onClick={buttonPressed}>Btn</button>
Yulian Kraynyak
  • 369
  • 4
  • 11