Home > Blockchain >  React onClick not firing on component
React onClick not firing on component

Time:12-11

I'm using Remix with React and TS. I have a simple class component called Map.tsx, that is used in index.tsx.

Map.tsx

class Map extends React.Component {
  constructor(props: any) {
    super(props);
    this.regionClicked = this.regionClicked.bind(this);
  }

  render() {
    return (
      <div>
        <button onClick={() => alert("ciao")}>a</button>
      </div>
    )
  }
}

index.tsx

import Map from "../components/index/map/Map";

class Index extends React.Component {
  constructor(props: any) {
    super(props);
  }

  render() {
    return (
      <div>
        <Map />
      </div>
    )
  }
}

CodePudding user response:

I'm not sure what is your problem. But in the file Map.tsx you need declare the method regionClicked or remove this line

class Map extends React.Component {
  constructor(props: any) {
    super(props);
    this.regionClicked = this.regionClicked.bind(this);
  }
  regionClicked() {
    alert("ciao");
  }
  render() {
    return (
      <div>
        <button onClick={this.regionClicked}>a</button>
      </div>
    );
  }
}

CodePudding user response:

Fixed by using the tag in the root.tsx file. Example available here

import { Scripts } from "remix";

function Document({ children }: { children: React.ReactNode; title?: string }) {
  return (
    <html lang="en">
      <head>
        <Meta />
        <Links />
      </head>
      <body>
        <Header />
        {children}
        <Footer />
        <Scripts />
        {process.env.NODE_ENV === "development" ? <LiveReload /> : null}
      </body>
    </html>
  );
}
  • Related