Javascript 反应导航:从导航器调用screen的函数-可能吗?

Javascript 反应导航:从导航器调用screen的函数-可能吗?,javascript,react-native,react-navigation,Javascript,React Native,React Navigation,我一直在尝试从导航器的屏幕调用导航器屏幕中的函数 为了澄清这一点,这里是我的一段代码 //ScreenA.js export default class ScreenA extends React.Component { showWarning(){ this.setState({showWarning: true}); setTimeout(function() { this.setState({showWarning: fals

我一直在尝试从导航器的屏幕调用导航器屏幕中的函数

为了澄清这一点,这里是我的一段代码

//ScreenA.js
export default class ScreenA extends React.Component {
    showWarning(){
        this.setState({showWarning: true});
        setTimeout(function() {
            this.setState({showWarning: false});
        }.bind(this), 3000);
    }

    render() {
        return (
          <View  style={{backgroundColor : this.state.showWarning ? "#red" : "#blue"}}>
              {this.state.showWarning && <Warning />}
          </View>
        )
    }
}

//Nagigator.js
default export const Navigator = StackNavigator({
  ScreenA: {screen: ScreenA},
  ScreenB: {screen: ScreenB},
});

//App.js
export default class App extends React.Component {
    handleSubmit(qrCode){
        if(qrCodeIsInvalid){
            this.navigator.ScreenA.showWarning();
            //This is just a sudo code.
            //How do we call ScreenA.showWarning here?
        }
    }


    render() {
        let props = {/*some props here*/};            
        return (//This "ref" stops the application without describing any reason
            <Navigator screenProps={props} ref={nav => { this.navigator = nav; }}/>
        );
    }
}
//ScreenA.js
导出默认类ScreenA扩展React.Component{
showWarning(){
this.setState({showWarning:true});
setTimeout(函数(){
this.setState({showWarning:false});
}.bind(this),3000);
}
render(){
返回(
{this.state.showWarning&&}
)
}
}
//Nagigator.js
默认导出常量导航器=堆栈导航器({
ScreenA:{screen:ScreenA},
ScreenB:{screen:ScreenB},
});
//App.js
导出默认类App扩展React.Component{
handleSubmit(qrCode){
如果(qrCodeIsInvalid){
this.navigator.ScreenA.showWarning();
//这只是一个sudo代码。
//我们怎么称呼ScreenA.showWarning?
}
}
render(){
让props={/*这里的一些props*/};
return(//此“ref”停止应用程序而不说明任何原因
{this.navigator=nav;}}/>
);
}
}
这里有一个示例,说明如何从导航调用函数,而不是从导出导航器的类调用函数

我认为每个屏幕都可以通过访问,但这会导致错误,而不会解释发生了什么

有没有人遇到过类似的情况

如有任何建议,将不胜感激

p.S.

@尼姆罗德·阿尔戈夫

下面是我一直在努力实现的细节

ScreenA
具有二维码阅读器和
submit
功能,可将二维码提交到
App.js

App.js
具有handleSubmit功能,在该功能中,提交的二维码将被发送到服务器并标记为有效或无效

如果提交的二维码结果无效,
ScreenA
必须显示警告消息并更改其背景颜色3秒钟

这可以通过让
App.js
在3秒内将一个道具
{showWarning:true}
传递给
ScreenA
并传递
{showWarning:false}
来实现

然而,我认为改变背景颜色是ScreenA的责任。因此,我在
showWarning()
中设置了
setTimeout
setState
,我是这样做的:

ScreenA.js 同时导航到ScreenB,包括要调用的函数

导出默认类ScreenA扩展React.Component{
建造师(道具){
超级(道具);
this.doSomething=this.doSomething.bind(this);
}
doSomething(){
this.setState({blah:true});
}
导航选项卡(){
this.props.navigation.navigate('ScreenB'{
doSomething:this.doSomething,
});
}
}
ScreenB.js 所以你可以在ScreenB中这样做

const{state,setParams,navigate}=this.props.navigation;
const params=state.params | |{};
参数doSomething();

在我看来,你似乎在试图强迫别人做某件事,而这可能不是最好的主意。但是,您询问的是实施情况。你能详细解释一下吗?@NimrodArgov谢谢,我在原始描述中添加了细节。谢谢你的建议。不幸的是,当
navigation.navigate()
位于标题内(即
静态导航选项
)时,这种方法似乎不起作用。@Hiroki这是关于如何访问
道具.navigation
的另一个主题。这里有答案,非常感谢。I+1ed在post上。从屏幕A到屏幕C可以吗?