1

Passing a dynamic property on onClick() by the use of ref gives me back: TypeError: _this.listReference is null listReference is defined in my component.

In Component #1

class Component1 extends Component {
    constructor(props){   
        super(props)
        this.listReference= null;
}
//Returns
<div>
  <SomeComponent list={(ref) => 
    this.listReference= ref} />
  <Component2 onMarkerClick = {(index) => {                
    this.listReference.scrollTop = 48 * index
   }}/> 

In Component #2

render() {
const {classes, driversStore, onMarkerCLick} = this.props

...

{driversStore.sortedSelectedOrders.map((order , index) => {
      return (
         <Component3 onClick={() => 
          onMarkerClick(index)} />

In Component #3


  render() {
    const { onClick } = this.props;
    return (
      <div
        onClick={onClick}>

I expect upon click to trigger the scroll functionality (as Stated in Component #1). Thanks in advance!

3
  • Did you forget doing this.listReference = React.createRef(); in your constructor and referring it somewhere in your component as return <div ref={this.listReference} />; ? Commented Mar 27, 2019 at 8:54
  • 1
    Please create a Minimal, Complete, and Verifiable example so that it will be easier for someone to help you. Commented Mar 27, 2019 at 8:58
  • @G_S I have refered it. (forgot to include it in the code) ill edit my code so you can check again. thanks Commented Mar 27, 2019 at 9:10

1 Answer 1

1

Check this example. Hope it can help you!

const Component2 = (props) =>(
  <button onClick={props.onClick}>click me</button>
);

const SomeCompo = (props) =>(
  <div>SomeComponent</div>
);

class Component1 extends React.Component{
    listReference = React.createRef();
  
    render(){
      return(
        <div>
          <SomeCompo list={this.listReference}>reference</SomeCompo>
          <Component2 onClick={this.handleClick} />
        </div>
      );
    }
    
    handleClick = () => {
    
      if(this.listReference){
       this.listReference={scrollTop:100};
      }
      console.log(this.listReference)
    }
}

ReactDOM.render(<Component1/>,document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>

You should do the following in constructor,

this.listReference = React.createRef()

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

3 Comments

Hey. I tried this.listReference = {} in return the console doesnt respond when i click. so its being rendered as empty
Check the example if it can help you!
Thanks alot for your Support!

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.