0

I have two components. They both render fine. Component one is inside component two. A button inside a box.

When I click the button I want to alert('hello'), but no success... However, if I insert the button-component alone in class Main it works...

Component one:

class Button extends Component {
  sayHello() {
    alert("hello!");
  }

  render() {
    return (
      <div>
        <button onClick={() => this.sayHello()}>PRESS</button>
      </div>
    );
  }
}

export default Button;

Component two:

class Box extends Component {
  render() {
    return (
      <div>
        <h1>I just want say...</h1>
        <Button />
      </div>
    );
  }
}

export default Box;

index.js :

class Main extends React.Component {
  render() {
    return (
      <div>
        <Box />
      </div>
    );
  }
}

ReactDOM.render(<Main />, document.getElementById("root"));
4
  • Check console for any errors.. Commented Mar 20, 2020 at 13:18
  • this should work without any problem , check if there some erro in console ? Commented Mar 20, 2020 at 13:20
  • No errors in the console, just a webpack message: Waiting for update signal from WDS... Commented Mar 20, 2020 at 21:01
  • it only works if I place the component on its own, that is not as a child component in the box component... Commented Mar 20, 2020 at 21:22

1 Answer 1

2

My CSS styling changed the z-index making the button unclickable. Changed position absolute to relative, works fine now.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.