javascript - React native - call function to close modal from another component gives `undefined is not an object` - Stack Overf

I am trying to call function of parent class from child to close modal. However, I always get undefined

I am trying to call function of parent class from child to close modal. However, I always get undefined is not an object this.ref.modal

This is what I have:

1st 

Import Two from ‘./Two’;


export default class One extends Component {
    static closeModal(){
            this.refs.modal.close();
    }
    <Modal>
    </Two>
    </Modal>
}

2nd

Import One from ‘./One’;

export default class Two extends Component {
    randomFunction(){
       One.CloseModal();
}
}

First ponent is a modalbox and second ponent is Camera. I would like to close first modal from Camera ponent. Am I doing something wrong?

I am trying to call function of parent class from child to close modal. However, I always get undefined is not an object this.ref.modal

This is what I have:

1st 

Import Two from ‘./Two’;


export default class One extends Component {
    static closeModal(){
            this.refs.modal.close();
    }
    <Modal>
    </Two>
    </Modal>
}

2nd

Import One from ‘./One’;

export default class Two extends Component {
    randomFunction(){
       One.CloseModal();
}
}

First ponent is a modalbox and second ponent is Camera. I would like to close first modal from Camera ponent. Am I doing something wrong?

Share asked Jan 16, 2018 at 3:16 Krilo MaxKrilo Max 2751 gold badge3 silver badges14 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

What you are trying to do is have a parent ponent, One, pass a function to the child, Two, and have the child ponent call the function. The general way you should approach passing information, whether it be data or functions, from a parent to the child is through props. This could be achieved through the following approach:

One.js

import Two from ‘./Two’;

export default class One extends Component {
    closeModal(){
        this.refs.modal.close();
    }
    render() {
        return (
            <Modal>
                <Two closeModal={this.closeModal} />
            </Modal
        )
    }
}

Two.js

export default class Two extends Component {
    randomFunction() {
        this.props.closeModal()
    }
}

The key part is in One.js when I instantiate the Two ponent and pass in the closeModal function as a prop. Then, in Two.js you can access all props passed into the class within the "this.props" object.

Notice how I didn't have to import One.js in Two.js. This is because in React you should think of each ponent as its own entity that doesn't know anything about the parent class that is using it. Two.js just knows that it's parent will be using it and passing in a "closeModal" function as a prop which it can use.

You can read more about props and look at examples here. To read more about thinking hierarchically with react, you can look at this guide

Good luck!

Before using this.ref.modal you must set your ref like ref='modal'. And you also need to bind your function closeModal before passing it as a prop.

One.js

import Two from './Two'

export default class One extends Component {
    closeModal(){
        this.refs.modal.close();
    }

    render() {
        return (
            <Modal ref='modal'>
                <Two closeModal={() => this.closeModal()} />
            </Modal
        )
    }
}

Two.js

export default class Two extends Component {
    randomFunction() {
        this.props.closeModal()
    }
}

For more information refer to https://reactjs/docs/refs-and-the-dom.html and https://facebook.github.io/react-native/docs/direct-manipulation.html

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742403566a4437392.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信