DEV Community

Cover image for Ref error
Michael Li
Michael Li

Posted on

Ref error

React version:
16.8.6

Steps To Reproduce

import AUserList from '../AUserList';
class AUsers extends Component {

  constructor(props) {
    super(props);

    this.list = React.createRef();
    this.divTag = React.createRef();
  }
  handleChangeDataList = () => {
    console.log(this.list.current);//null
    console.log(this.divTag.current);//it works
   this.list.current.reloadAUserTable();//it is error
  }
  render() {
   return (
        <>
            <div className="animated fadeIn" ref={this.divTag}/>
            <AUserList ref={ref => this.list=ref}/>
            <Button  onClick={() => this.handleChangeDataList()}>atest</Button>
       </>
   )
  }
}
export default connect(mapStateToProps, mapDispatchToProps, null, {forwardRef : true})(AUsers);

class AUserList extends Component {
//from AUsers
  reloadAUserTable = () => {
    console.log('good');
  }
render() {
    const {
      AUserData_arr,
    } = this.props;
    return (
      <div>
        <Card>
          <CardBody>
            {AUserData_arr  &&
              <BootstrapTable data={AUserData_arr} version="4" striped hover pagination search tabIndexCell >

                <TableHeaderColumn dataField="UserLevel_Name" dataFormat={this.userLevelFormatter} dataSort>LEVEL</TableHeaderColumn>
              </BootstrapTable>
            }
          </CardBody>
        </Card>
      </div>
    );
  }

Top comments (3)

Collapse
 
dance2die profile image
Sung M. Kim • Edited

Hi Michael.

Is this a question? a post or a #devjournal?

Collapse
 
michaelli91 profile image
Michael Li • Edited

Hi, Sung M.
Thanks for your reply.
Yes, it is a question,
To be honest I am a beginner.
So If you can guide for me, it will be great pleasure.

Collapse
 
dance2die profile image
Sung M. Kim • Edited

The callback refs is what you've used to get the AUserList's ref.

In that case you don't access the ref with .current like this.list.current but as this.list.

You also don't create this.list with React.createRef() with callback ref.

Can you provide a minimal runnable sample with CodeSandbox.io or Stackblitz if it still doesn't work?