【问题标题】:How to access this.props in the react navigation?如何在反应导航中访问 this.props?
【发布时间】:2018-04-03 06:05:12
【问题描述】:

我在我的 React Native 应用程序中使用反应导航,我想在用户单击应用程序右上角的保存按钮时调用函数 onSavePress()。

但是当我尝试调用 onSavePress 函数时,我的应用程序崩溃了,换句话说,它不像我从静态 navigationOptions 函数内部调用该函数。

下面是代码:

class TheFormScreen extends Component {

  constructor(props)
  {
    super(props);
    this.state = {
        visible: true
      };
  }

  state = {
      isReady: false,
    };


static navigationOptions = ({ navigation }) => ({
    title: `${navigation.state.params.item.a1}`,
    tabBarIcon:  ({ tintColor }) => {
          return <Icon name="phone" size={30} color={tintColor} />;
    },
    headerRight: (
      <Btn
        title='Done-'
        onPress={() => { console.log(' on Press done on line 73 Form--Screen    this.props=', this.props);  navigation.goBack()   }
         }
      />
    ),
  });

onDecline(){
  // console.log(' onDecline  clicked');
  this.setState ({ showModal : false });
}



}
onDeleteProperty() {
  // console.log(' onDelete  clicked');

}


onSavePress(){
    this.onClickToaster();
    console.log( '101 - on click onSavePress ');
    const { a1, a2 } = this.props;

    this.props.propertySave({  a1, a2 });
}

【问题讨论】:

标签: react-native react-navigation


【解决方案1】:

所以,这只是所有代码的一个清晰的 sn-p 广告 infoSave is no where 和 onSavePress 也是。

我假设您的问题是 this.props 未定义或类似的东西。如果是这种情况,您可以将 this 更改函数绑定为箭头函数:

onSavePress = () => {
    this.onClickToaster('Saving the info....', 'top' , DURATION.LENGTH_LONG, true);
    const { a1, a2 } = this.props;
    this.props.infoSave({  a1, a2 });
}

编辑:

你是说这个吗?

 headerRight: (
  <Btn
    title='Save'
    onPress={() => { this.onSavePress();  navigation.goBack()   }
     }
  />
),

【讨论】:

  • 因此问题是如何从反应导航的右上角调用 onSavePress 函数?
  • 查看我的编辑。尽管这很令人困惑,因为您显然假设了很多不在问题中的代码。所有这些代码都在同一个类中吗?同一个文件?为什么不工作?如果您可以添加更多信息,会更容易提供帮助
  • 是的,所有这些都在同一个类中。
猜你喜欢
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
  • 2020-07-15
  • 2018-09-20
  • 2017-10-25
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
相关资源
最近更新 更多