cmswalker / fullpage-react

Stateful fullpage.js inspired scrolling for React
https://cmswalker.github.io/fullpage-react/
206 stars 42 forks source link

Can't get ref in componentDidMount #99

Closed Henri-Zhang closed 6 years ago

Henri-Zhang commented 6 years ago

`fullPageOptions.slides = [

]` ... `componentDidMount() { console.log(this.refs.test) }` It's output an 'undefined', how can I get this ref?
cmswalker commented 6 years ago

My guess is you're not grabbing the ref in the CustomComponent class as a prop and passing it within the render function to a div input etc.

This has nothing to do w/ this library, it's just the implementation of refs in general. Also, if you're passing refs as props, use another name instead of ref

https://reactjs.org/docs/refs-and-the-dom.html#creating-refs

class MyComp extends React.Component {
      componentDidMount() {
        const { refs } = this;
        console.log({ refs });
      }

      render() {
        return <div ref={this.props.refName}>MyComp</div>;
      }
    }
<MyComp refName="test"/>