4

I am trying to trigger click button on another onClick event function.

Error : Uncaught TypeError: this.clickBack.current.click is not a function

React version is 16.8.6

constructor(props) {
    super(props)

    this.clickBack = React.createRef();
    this.startInterview = this.startInterview.bind(this);
}

startInterview(){
    console.log('hello') // output : hello
    console.log(this.clickBack.current); // output : Button {props: {…}, context: {…}, refs: {…}, updater: {…}, onClick: ƒ, …}
    this.clickBack.current.click(); // output : Uncaught TypeError: this.clickBack.current.click is not a function
}

In render -> return method

<Link to={backLink}>
    <Button ref={this.clickBack}> Back</Button>
</Link>
<Button onClick={this.startInterview}> Start Interview</Button>

2 Answers 2

4

as we can see in the console.log the ref is referencing the Button component, not a dom element.If this Button is a component you defined, you should pass your ref through a prop to the button dom element e.g.

<Button innerRef={this.clickBack}...>

Button.js

...
<button ref={this.props.innerRef}

(here innerRef is an arbitrary name)

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

Comments

4

You can try this:

this.clickBack.current.link.click()

instead of:

this.clickBack.current.click()

1 Comment

No, didn't work :(

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.