admin管理员组文章数量:1336087
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 badges2 Answers
Reset to default 6What 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
本文标签:
版权声明:本文标题:javascript - React native - call function to close modal from another component gives `undefined is not an object` - Stack Overf 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742403565a2468365.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论