如何进行导航。

发布于 2025-02-12 19:54:34 字数 1036 浏览 0 评论 0原文

我正在使用Typescript设置堆栈导航,这是我第一次使用Typescript。我有一个屏幕login.tsx我想自定义导航栏。

import { useNavigation } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
import React from 'react';
import { SafeAreaView, Text } from 'react-native';
import { RootStackParamList } from '../typings';

type loginScreenProps = StackNavigationProp<RootStackParamList, 'Login'>;

const Login = () => {

  const navigation = useNavigation<loginScreenProps>();

  navigation.setOptions  = {
    headerTitle: 'Demo', // <-- ERROR
  }

  return (
    <SafeAreaView>
        <Text>Login</Text>
    </SafeAreaView>
  )
}

export default Login

我要去的错误是:

type'{headerTitle:string; }'不能分配输入'(选项: 部分)=&gt;空白'。对象字面可能只能 指定已知属性,并且“ headertitle”不存在类型 '(选项:部分)=&gt; void'。

rootstackParamlist是简单offor type rootstackParamlist = {login:undefined; ...

如何在Typescript中实现它?

I'm setting up stack navigation using typescript and it's my first time working with typescript. I have a screen Login.tsx in which I want to customize the navigation bar.

import { useNavigation } from '@react-navigation/native';
import { StackNavigationProp } from '@react-navigation/stack';
import React from 'react';
import { SafeAreaView, Text } from 'react-native';
import { RootStackParamList } from '../typings';

type loginScreenProps = StackNavigationProp<RootStackParamList, 'Login'>;

const Login = () => {

  const navigation = useNavigation<loginScreenProps>();

  navigation.setOptions  = {
    headerTitle: 'Demo', // <-- ERROR
  }

  return (
    <SafeAreaView>
        <Text>Login</Text>
    </SafeAreaView>
  )
}

export default Login

The error I'm going is:

Type '{ headerTitle: string; }' is not assignable to type '(options:
Partial) => void'. Object literal may only
specify known properties, and 'headerTitle' does not exist in type
'(options: Partial) => void'.

The RootStackParamList is simple expor type RootStackParamList = { Login: undefined;...

How do I achieve it in typescript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城缀染半城烟沙 2025-02-19 19:54:34

navigation.setoptions是一个函数,将对象作为输入

尝试进行此更改,并让我知道它是否已固定

navigation.setOptions({
  headerTitle: 'Demo',
})

navigation.setOptions is a function which takes object as an input

Try making this change and let me know if its fixed

navigation.setOptions({
  headerTitle: 'Demo',
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文