如何通过React Native Web通过react-navigation设置文档标题?

广贺

我的Web应用程序是由React Native Web使用react-navigator创建的。

默认情况下,react-navigator将RouteName设置为document.title。

例如

<Stack.Navigator screenOptions={{ headerShown: false }}>
    <Stack.Screen name="root" component={RootScreen} />
    <Stack.Screen name="search" component={SearchScreen} />
</Stack.Navigator>

产生

...
<head>
  <title>root</title>
  ...

我想要的是

按需更改document.title。

我试过但没用的

尝试直接访问文档对象,但是下面的代码不起作用。

export default function RootScreen({ navigation }) {
  useEffect(() => {
    document.title = `My Web App | ${someMessage}`
  })

埃里克卢维

我发现Expo for Web基本上将屏幕标题设置为文档标题。所以我做了这样的事情:

import { Platform } from 'react-native';
const title = (text) => Platform.select({ web: `My Web App | ${text}`, default: text });
<Stack.Navigator screenOptions={{ headerShown: false }}>
    <Stack.Screen name="root" component={RootScreen} options={{ title: title('root') }} />
    <Stack.Screen name="search" component={SearchScreen} options={{ title: title('search') }} />
</Stack.Navigator>

然后,您可以修改该title功能以返回各种屏幕的预期标题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

React Native:设置 React Navigation 的屏幕标题

如何在 React Native 中通过点击设置动态样式?

在React Native中通过获取设置状态

如何在React Native React Navigation 5中为标题后退按钮的颜色设置动画?

如何在React-Native中通过React-Navigation多次传递参数?

React Native如何通过onChangeText传递道具

如何通过React Native访问Amazon Sagemaker?

如何通过React Web页面对React Native用户进行身份验证?

Firestore-如何在React Native中通过ID获取文档

如何使用 React Native 监控 webview 文档标题的变化

在React Native中通过props从数组设置文本

通过React Native覆盖组件

如何在React Native中设置多行标题

在React Native中如何通过WebView访问摄像头

react-native firebase 如何通过 id 删除特定节点?

React Native:如何通过警报扩展此计时器?

如何在React Native中通过Paypal进行汇款?

我如何在React Native中通过此对象映射?

如何通过ref访问react-native listview子组件?

如何通过WebView方法在React-Native上旋转视图

如何通过React Native应用打开外部应用?

如何通过 Typescript for Flow 导入 React-Native 类型?

如何使用react-native通过WebView下载?

如何在React Native中通过Toggle更改主题颜色?

如何动态设置推送屏幕的方向-React-Native-Navigation

无法在React Native中动态设置标题标题

您如何在React中设置文档标题?

通过adb tcpip调试React Native

通过React Native更改图标activeIndex