【问题标题】:Is there a way to set parameters to navigation in a screen befor navigationOption load in react native 0.60有没有办法在导航选项加载之前在屏幕中设置导航参数以响应本机 0.60
【发布时间】:2019-10-13 06:47:09
【问题描述】:

我有一个编辑页面,当用户选择所需的颜色和图标时,单击屏幕标题上的保存按钮,此选定的颜色和图标保存在 Asyncstorage 上并返回主页。 我将颜色和图标存储在编辑页面的状态中,并且我有一个名为 SaveChanges() 的函数,该函数将此颜色和图标保存在 Asyncstorage 上并返回到主页。 我想在单击标题上的保存按钮时调用此函数。 但在导航选项中:
1-我无法访问这个(作为这个页面)
2-navigationOption在componentDidMount之前调用,所以componentDidMount中的setParam对navigationOption没有影响。


componentDidMount() {

     this.props.navigation.setParams({ saveChanges: this.saveChanges });
   }

saveChanges=()=>{
     console.warn("in save changes");
     asyncstorage.SetinfoInAsyncstorage(this.props.lable,{issuer:this.state.issuerName,color:this.state.selectedColor,icon:this.state.selectedIcon}, (mymessage) =>
     {
        const resetAction = StackActions.reset({
          index: 0,
           actions: [NavigationActions.navigate({ routeName: 'AllAccountInfo',params:{allaccountmessage:mymessage} })],
          });
        this.props.navigation.dispatch(resetAction);



     });
   }

   static navigationOptions = ({ navigation }) => {
     return {
       headerRight: (
         <MaterialHeaderButtons>
           <Item buttonStyle={{color:'black'}}  title="save" iconName="save" onPress={()=>{
             navigation.getParam('saveChanges');
             }} />
         </MaterialHeaderButtons>

       ),
     };
   }; 

导航的文档说我必须使用redux。但我不想要 redux,因为它使我的应用程序变得复杂。
当我搜索时,标题的标题会动态变化,但我看不到 headerRight。

【问题讨论】:

  • 我找到了答案:)
  • 请上传您的答案,因为其他用户可能会发现它对将来参考有用

标签: react-native


【解决方案1】:

在 componentDidMount 中的 this.props.navigation.setParams 中,我设置了我需要在 headerRight 中显示的所有内容,而不仅仅是一个函数。

var that = this;
  this.props.navigation.setParams({ headerRight: 
  <MaterialHeaderButtons>
    <Item buttonStyle={{color:"white"}}  title="save" iconName="save" onPress={
      ////save color icon and application_name name in asyncstorage
      function(){
       asyncstorage.SetAccountinfoInAsyncstorage(that.user_id,{application_name:that.state.application_name,color:that.state.selectedColor,icon:that.state.selectedIcon}, (mymessage) =>
       {
          const resetAction = StackActions.reset({
            index: 0,
             actions: [NavigationActions.navigate({ routeName: 'AllAccountInfo' })],
            });
            that.props.navigation.dispatch(resetAction);



       });

      }} />
  </MaterialHeaderButtons>,
  primaryColor : this.props.theme.colors.primary,
  textColor:this.props.theme.colors.text,
  surfaceColor:this.props.theme.colors.surface,
  dark:this.props.theme.dark
  });

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-09
    相关资源
    最近更新 更多