Call child function from parent component in React Native

前端 未结 5 452
梦毁少年i
梦毁少年i 2020-12-03 09:47

I\'m developing my first React Native app. What I\'m trying to achieve is to execute a child function from the parent component, this is the situation:

Child

5条回答
  •  情歌与酒
    2020-12-03 10:25

    it is in react. i hope it may help you.

    class Child extends React.Component {
      componentDidMount() {
        this.props.onRef(this)
      }
      componentWillUnmount() {
        this.props.onRef(null)
      }
      method() {
        console.log('do stuff')
      }
      render() {
        return 

    Hello World!

    } }

    class EnhancedChild extends React.Component {
            render() {
            return 
          }
        }
    
    class Parent extends React.Component {
      onClick = () => {
        this.child.method() // do stuff
      };
      render() {
        return (
          
    (this.child = ref)} />
    ); } } ReactDOM.render(, document.getElementById('root'))

    Original Solution:

    https://jsfiddle.net/frenzzy/z9c46qtv/

    https://github.com/kriasoft/react-starter-kit/issues/909

提交回复
热议问题