5

my App component like as below:

class App extends Component {
  clickHandler = () => {
    console.log('click');
  }
  render() {
    return (
      <div className="App">
        <div onClick={this.clickHandler}>Test1</div>
        <Person onClick={this.clickHandler}>Test2</Person>
      </div>

    );
  }
}

And this is my Person component:

class Person extends Component {
    render() {
        return (
            <div>{this.props.children}</div>
        )
    }
}

When i click on Test1 onClick works and show click on console, but when i click on Test2 onClick doesn't work. how to i handler onClick in parent component? i don't want pass onClick to child component.

1
  • Why don't you want to pass onClick to the child component? That's pretty much how React works, although there are different ways to get properties in children. Commented Nov 24, 2018 at 19:25

4 Answers 4

5

This should work. Why don't you want to write it this way? You ARE passing it as a prop...

<div onClick={props.onClick}>Test</div>
Sign up to request clarification or add additional context in comments.

Comments

2

You need to pass onClick to the <div> in Person.

onClick is not a special prop that works everywhere in React - The only intrinsic props are key and children. Everything else needs to be wired up manually.

The reason your code doesn't work is because you're passing onClick to Person, but to the Person component, onClick is just another prop. It does nothing on it's own until you pass it to a component that understands what it means to handle a click event (which all of the built-in components in React do).

Comments

1

Here is the complete solution... Checkout: https://codesandbox.io/s/vjp200rj3

Modified render function in App.

render() {
    return (
      <div className="App">
        <div onClick={this.clickHandler}>Test1</div>
        <Person clickHandler={this.clickHandler}>Test2</Person>
      </div>
    );
  }

Modified render function in Person:

  render() {
    return <div onClick={this.props.clickHandler}>{this.props.children}</div>;
  }

Comments

0
class Person extends Component {
    render() {
        return (
            <div onClick={propes.onClick}>{props.children}</div>
        )
    }
}

1 Comment

Thanks for your contribution. Please add an explanation, highlighting parts of your answer that addresses OP's issue, & why/how. Code only answers are generally frowned upon on SO. Adding details adds to long term value, & helps future visitors learn, so they can apply this knowledge to their own coding issues. Also, it improves quality of the answer, & hence SO as well. Higher quality answers are upvoted more often, as more users find them a good resource to refer back to. Consider editing to provide additional info, explain why you chose this approach, or highlight what OP needed to change.

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.