如何在 tabNavigator (React Native) 的 if 语句中调用驻留在父屏幕中的函数
How to call a function residing in a parent screen within an if statement in a tabNavigator (React Native)
所以我的问题是:父组件是选项卡导航器中的一个屏幕,其中嵌套了另一个选项卡导航器。我可以通过 screenProps 将道具向下传递给子选项卡。但是,我无法从子选项卡导航器中的任何屏幕调用父屏幕中的函数。我究竟做错了什么?
ParentScreen.js //One of the screens of a parent tab navigator
import React, { Component } from 'react';
import { ChildTabs } from '../navigatation/router.js';
export default class ParentScreen extends Component {
state = { name: '' }
componentWillMount() {
this.parentFxn();
}
parentFxn() {
this.setState({ name: 'Staker' });
}
render() {
screenProps = { parentFxn: this.parentFxn.bind(this) };
return(
<ChildTabs // this is a tab Navigator
screenProps={screenProps}
/>
);
}
}
现在子屏幕之一:
ChildScreen.js
//childScreen is one of the screens in the tab navigator
import React, { Component } from 'react';
import {Button} from 'react-native-elements';
import { ChildTabs } from '../navigatation/router.js';
export default class ChildScreen extends Component {
childFxn() {
axios.get(someRemoteUrl)
.then((response) => {
if (response.status == 200) {
return this.props.screenProps.parentFxn; //this never gets called
}
});
}
render() {
return(
<Button
onPress={this.childFxn.bind(this)}
/>
);
}
}
函数没有被调用,因为你没有调用它。应该这样称呼
if (response.status == 200) {
return this.props.screenProps.parentFxn(); //this always gets called
}
所以我的问题是:父组件是选项卡导航器中的一个屏幕,其中嵌套了另一个选项卡导航器。我可以通过 screenProps 将道具向下传递给子选项卡。但是,我无法从子选项卡导航器中的任何屏幕调用父屏幕中的函数。我究竟做错了什么?
ParentScreen.js //One of the screens of a parent tab navigator
import React, { Component } from 'react';
import { ChildTabs } from '../navigatation/router.js';
export default class ParentScreen extends Component {
state = { name: '' }
componentWillMount() {
this.parentFxn();
}
parentFxn() {
this.setState({ name: 'Staker' });
}
render() {
screenProps = { parentFxn: this.parentFxn.bind(this) };
return(
<ChildTabs // this is a tab Navigator
screenProps={screenProps}
/>
);
}
}
现在子屏幕之一:
ChildScreen.js
//childScreen is one of the screens in the tab navigator
import React, { Component } from 'react';
import {Button} from 'react-native-elements';
import { ChildTabs } from '../navigatation/router.js';
export default class ChildScreen extends Component {
childFxn() {
axios.get(someRemoteUrl)
.then((response) => {
if (response.status == 200) {
return this.props.screenProps.parentFxn; //this never gets called
}
});
}
render() {
return(
<Button
onPress={this.childFxn.bind(this)}
/>
);
}
}
函数没有被调用,因为你没有调用它。应该这样称呼
if (response.status == 200) {
return this.props.screenProps.parentFxn(); //this always gets called
}