javascript - 将 Prop 从 Modal 传递到其他组件 react 导航

标签 javascript react-native react-navigation

我使用 react navigation .我有一个 TabNavigator .每个Tab包含 StackNavigator .来自一个StackNavigator , 可以打开 Modal . Modal当我点击 Button 时打开在某个Component .

export default class CallModalComponent extends Component {
    constructor(props) {
        super(props)
    ...
    }

    ...

    render() {
    const { navigate } = this.props.navigation;
         return (
              <Button
               ....
               onPress={() => navigate("Modal")}/>

TabNav 中的注册画面<MyModal />是有状态的 Component . 在 Modal 结束时我需要 state<MyModal />将传递给 <CallModalComponent /> .

我遇到的问题是如何使用 react navigation在两者之间...我知道我可以使用 redux并通过 global store 发送/检索它.但我想知道它是否可能只有 react native . 有什么建议吗?

编辑

我从答案中实现了代码

export default class CallModalComponent extends Component {
    constructor(props) {
        super(props)
    ...
    }

    ...
    onModalDismis(childVar) {
      console.log('modal is closing');
      console.log(childVar);
    }
    render() {
    const { navigate } = this.props.navigation;
         return (
              <Button
               ....
               onPress={(childVar) => navigate("Modal", {onModalDismis: this.onModalDismis()})}/>

// Then in your modal component

componentWillUnmount () {
    console.log('unmount');
    this.props.navigation.state.params.onModalDismis('here we go');
}

记录以下内容: 当 Modal Component已安装我得到:

modal is closing undefined

然后,当我实际关闭 Modal 时,我得到:

unmount

然后是错误:

Cannot read property of onModalDismiss of undefined.

我预计在安装 Modal 时不会记录任何内容.然后,当我关闭 Modal我以为

unmount , modal is closinghere we go被记录。

最佳答案

您可以在导航时将参数传递给屏幕。这允许您将功能发送到下一个屏幕,然后您可以在需要时启动它。更多详情 here .

示例

export default class CallModalComponent extends Component {
    constructor(props) {
        super(props)
    ...
    }

    ...
    onModalDismis() {
      console.log('modal is closing');
    }
    render() {
    const { navigate } = this.props.navigation;
         return (
              <Button
               ....
               onPress={() => navigate("Modal", {onModalDismis: this.onModalDismis})}/>

// Then in your modal component

componentWillUnmount () {
    this.props.navigation.state.params.onModalDismis();
}

关于javascript - 将 Prop 从 Modal 传递到其他组件 react 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46240004/

相关文章:

react-native - 更新的代码未在 React Native Simulator 中显示

javascript - 在 React Native 中构建应用程序后出现空白屏幕

reactjs - 如何在 React-Navigation-3 中设置应用程序容器?

javascript - 使用 jQuery 水平滑动的垂直 slider

javascript - 在 Fusion Tables 和 Google Maps API 中,如何根据下拉菜单选择更改表格图层和 map 位置?

javascript - 检测国外网站参数变化

javascript - React Native - 渲染没有返回任何内容

javascript - 通过单击从数据库中删除特定条目

xcode - React Native/Xcode 升级,现在找不到 RCTConvert.h

https - React-native 0.40+ 获取自签名证书