小编典典

如何使用Typescript在无状态组件上设置navigationOptions

reactjs

我有一个类似的反应成分

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

使用react-navigation我通常会通过说来应用导航选项

Example.navigationOptions = { options };

使用打字稿我得到了错误

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

我怎样才能解决这个问题?

我尝试编写一个界面

interface ExampleWithNavigationOptions extends Element {
    navigationOptions?;
}

但是没有运气。


阅读 389

收藏
2020-07-22

共1个答案

小编典典

关键思想是为Example常量指定正确的类型。可能react-navigation已经提供了该类型。但是,您也可以这样扩展内置React接口:

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

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

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

    Example.propTypes = {
        /*
        ...
        */
    }
2020-07-22