0

I have two components, the parent and child. Currently I have these codes below. But unfortunately it returns an error:

TypeError: Cannot read property 'click' of null

For some reasons I want when button is click the Item component also will be click. But these codes below produces an error above. Anyone does know how to achieve it? import React, { useRef } from 'react';

const App = (props) => {
  const itemRef = useRef(null);
  
  return (
    <div>
    {dynamicBoolean ? (
         <button onClick={() => itemRef.current.click()}>
            click item
        </button>
    ) : (
        //more codes here
        <Item ref={itemRef} />
    )}  
    </div>
  );
};

export default App;

Child component would look like below (demonstration purposes, the code is very lengthly)

import React from 'react';

const Item = (props) => {
  
  return (
    <div>
        //some design here
    </div>
  );
};

export default Item;
5
  • You forget to use useRef. That should be the trick. Functional components handle that differently than class components. reactjs.org/docs/… Commented Mar 31, 2021 at 7:01
  • @Felix sorry forgot to put in the codes. now updated. Commented Mar 31, 2021 at 7:02
  • Can you show the Item componet? Commented Mar 31, 2021 at 7:03
  • 1
    Item will need to use forwardRef to forward the ref to the DOM element Commented Mar 31, 2021 at 7:03
  • 1
    @ZunayedShahriar included the child in the description :) Commented Mar 31, 2021 at 7:06

2 Answers 2

6

You need useRef and you have to forward this ref to the Item component.

import React, { forwardRef, useRef } from 'react';

const Item = forwardRef((props, ref) => {
  return <li {...props}
  onClick={() => alert('clicked on Item')}
  ref={ref} >MyItem</li>
})

const App = (props) => {
  const itemRef = useRef(null);
  
  return (
    <div>
     <button onClick={() => itemRef.current.click()}>
        click item
    </button>
     
     
      <Item ref={itemRef} />
    </div>
  );
};

export default App;
Sign up to request clarification or add additional context in comments.

1 Comment

I think this would solve my problem sir, but can't verify. Because current implementation is using conditional (see updated description), causing it to still display the same error.
1
import React, { createRef } from "react";

const Hello = (props) => {
  const itemRef = createRef();
  const hello = () => {
    itemRef.current.click();
  };
  return (
    <div>
      <button onClick={() => hello()}>click item</button>
      <Item ref={itemRef} />
    </div>
  );
};

const Item = React.forwardRef((props, ref) => {
  const myClick = () => {
    console.log("this is clicked");
  };
  return (
    <button ref={ref} className="FancyButton" onClick={myClick}>
      {props.children}
    </button>
  );
});

export default Hello;

1 Comment

You can use createRef , and then click that element as shown above. Cheers!

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.