我有一个react 成分,比如

const Example = () => (<View>...</View>);

使用react-navigation ,我通常会通过以下方式应用导航选项:

Example.navigationOptions = { options };

使用typescript,我得到了错误

[ts] Property 'navigationOptions' does not exist on type '(props: Props) => Element'.

我该怎么解决这个问题?

我试着写一个界面

interface ExampleWithNavigationOptions extends Element {
    navigationOptions?;
}

但是没有运气.

推荐答案

关键的 idea 是为Example常量指定正确的类型.也许react-navigation已经提供了这种类型.但是,您也可以像这样扩展内置React接口smth:

    interface NavStatelessComponent extends React.StatelessComponent {
        navigationOptions?: Object
    }

    const Example: NavStatelessComponent = () => {
        return (
            <View>
               ...
            </View>
        )
    }

    Example.navigationOptions = {
        /*
        ...
        */
    }

    Example.propTypes = {
        /*
        ...
        */
    }

React-native相关问答推荐

react 本机函数不通过usContext触发

如何在Reaction本地日历中 Select 年份?

在我的 React Native 应用程序中获取数据返回 [object Object] 而不是 renderSectionHeader

ios 中节列表中的scrollToLocation 不起作用.在 Android 上,列表滚动回索引 0,但在 ios 上不会发生同样的情况

如何将 react-native 图像从 expo-image-picker 上传到使用 multer 的 Express.js 后端

应用程序主体尚未注册

React Native - Expo:fontFamily 'SimpleLineIcons' 不是系统字体,尚未通过 Font.loadAsync 加载

react-native 签名 [AppName]Tests 需要开发团队 ios

react-native-snap-carousel 无法正确渲染

React.js - 为所有组件方法使用属性初始化器

如何在StackNavigator 中实现自定义标题图标?

什么时候适合在 React 中使用构造函数?

React Native:错误:找不到资源 android:style/TextAppearance.Material.Widget.Button.Borderless.Colored

React-Native + crypto:如何在 React-Native 中生成 HMAC?

React native base headers for ios not found

java.lang.ClassNotFoundException:在路径上找不到类.MainActivity:DexPathList react-native

如何从 React Native-App 中的 res/drawable-folder 加载图像?

任务在 react-native 中因请求而成为orphaned - 这是什么意思?

Ipad 上的 React Native - 错误 - could not connect to development server

React Native onLayout 与 React Hooks