react-native - 有没有办法在导航选项加载之前在屏幕中设置导航参数以响应本机 0.60
问题描述
我有一个编辑页面,当用户选择所需的颜色和图标时,单击屏幕标题上的保存按钮,此选定的颜色和图标保存在 Asyncstorage 上并返回主页。我将颜色和图标存储在编辑页面的状态中,并且我有一个名为 SaveChanges() 的函数,该函数将此颜色和图标保存在 Asyncstorage 上并返回到主页。我想在单击标题上的保存按钮时调用此函数。但是在 navigationOption:
1-我无法访问 this(as this page)
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。
解决方案
在 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
});
推荐阅读
- xml - 如何使用 XSLT 从平面 XML 文件创建 HTML 列表(基于早先的问题)
- linkedin - 哪个版本的 UGC 文档是正确的?
- python - 追加 numpy 一维数组不会导致矩阵
- java - 如下图所示创建页面(关于/我的主页/参考标签)的最佳方式是什么?
- pandas - 如何从列中完全删除一个元素?
- windows - @echo off 在批处理脚本中不起作用
- amazon-dynamodb - 为包含数百万个项目的 DynamoDB 表中的每个项目添加一个属性
- pandas - 使用下面的代码选择列但只获得第一行
- css - 如何使用外部css文件中的less添加类
- sql - Hive 查询:根据优先级和日期计算最大指标值