Skip to content

Instantly share code, notes, and snippets.

@hungdev
Last active May 29, 2018 04:07
Show Gist options
  • Select an option

  • Save hungdev/d4aa778b8db27db30dfda40e1fe4d749 to your computer and use it in GitHub Desktop.

Select an option

Save hungdev/d4aa778b8db27db30dfda40e1fe4d749 to your computer and use it in GitHub Desktop.
can not call refs because...

in some case, you cant call refs and get error:

undefined is not a function(evaluating'_this.refs.ModalPicker.onOpen()')

to solve this issue.

comment this lines

// const mapStateToProps = (state) => {
//   return {
//   }
// }

// const mapDispatchToProps = (dispatch) => {
//   return {
//   }
// }

// export default connect(mapStateToProps, mapDispatchToProps)(ModalPicker)

and

export default class

note: CAN NOT call refs in willMount, but you CAN call it in didMount

không thể gọi ref khi child component có redux:

Nôm na HOC là hàm để thay đổi component này thành component khác. (React Higher Order Components)

Hàm connect của react-redux cũng là HOC

Kỹ thuật này sẽ return component mới và pass props của component cũ vào. Mà ref gần như không phải prop nên bị mất.

React 16 thì có forwardRef tiện hơn

example:

class Foo extends React.Component {
  componentDidMount() {
     this.props.onRef(this);
  }

  render() {
     return 
  }
}
export default Foo;
import Foo from './foo';

class App extends React.Component {
onClick = () => {
    this.child.method() // do stuff
  }
   render() {
     <Foo onRef={foo => this.child=foo} />
     <button onClick={this.onClick}>Child.method()</button>
  } 
}

kriasoft/react-starter-kit#909 (comment) facebook/react#9348 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment