gpt4 book ai didi

javascript - 参数未传递且导航器在 createStackNavigator 中不可用 - React Navigation

转载 作者:行者123 更新时间:2023-11-28 03:11:32 24 4
gpt4 key购买 nike

我在我的应用程序中使用React Navigation。我在标题中使用 createBottomTabNavigator 创建了 createStackNavigator 我试图访问我从屏幕传递的参数,但这些参数不可用。我还希望当有人单击“标题”按钮然后转到新屏幕时,但问题是“navigation.navigator()”功能在标题按钮中不可用。

这是我的 Nav.js 的样子:

import React from 'react';
import {View, Text, Button} from 'react-native';
import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import { createBottomTabNavigator } from 'react-navigation-tabs';

import Screen1 from './Screen1';
import Screen2 from './Screen2';
import Screen3 from './Screen3';

const bottomTab = createBottomTabNavigator(
{
openOrder: Screen1,
closeOrder: Screen2
}
);

const mainNav = createStackNavigator({
order: {
screen: bottomTab,
navigationOptions: ({navigation}) => ({
headerRight: (
<View>
<Button title="Map" onPress={() => console.log(navigation)} />
</View>
)
})
},
map: Screen3
});

export default createAppContainer(mainNav);

Screen1.js

import React, { useEffect } from 'react';
import { View, Text } from 'react-native';

const Screen1 = props => {
useEffect(() => {
props.navigation.setParams({
title: 'Some Content',
map: mapit,
});
}, []);

const mapit = () => {
console.log('Map it function');
};

return (
<View>
<Text>Screen 1</Text>
</View>
);
};

export default Screen1;

Screen2.js

import React from 'react';
import {View, Text} from 'react-native';

const Screen2 = () => {
return(
<View>
<Text>Screen 2</Text>
</View>
);
}

export default Screen2;

Screen3.js

import React from 'react';
import {View, Text} from 'react-native';

const Screen3 = () => {
return(
<View>
<Text>Screen 3</Text>
</View>
);
}

export default Screen3;

这里是Snack

最佳答案

您可以通过创建一个具有导航功能的按钮来解决这个问题。

withNavigation 是一个高阶组件,它将 navigation 属性传递到包装组件中。当您无法将 navigation 属性直接传递到组件中,或者不想在深度嵌套子组件的情况下传递它时,它非常有用。

示例

import React from 'react';
import { Button } from 'react-native';
import { withNavigation } from 'react-navigation';

class MyMoveButton extends React.Component {
render() {
return (
<Button
title="map"
onPress={() => {
this.props.navigation.navigate("map");
}}
/>
);
}
}

// withNavigation returns a component that wraps MyBackButton and passes in the
// navigation prop
export default withNavigation(MyMoveButton);

使用

import MyMoveButton from "path for MyMoveButton"
...

headerRight: (
<View>
<MyMoveButton />
</View>
)

关于javascript - 参数未传递且导航器在 createStackNavigator 中不可用 - React Navigation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60069419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com