gpt4 book ai didi

reactjs - 函数组件中的 React/ReactNative 静态属性

转载 作者:行者123 更新时间:2023-12-03 14:30:45 28 4
gpt4 key购买 nike

在我的 React Native 编写的 TypeScript 中,我使用 ReactNavigation。在我使用如下的类组件之前

class HomeScreen extends Component {
static navigationOptions: any;
...
}

// in navigation config
HomeScreen.navigationOptions = {...}

通过 hooks 支持,我将 HomeScreen 迁移到如下功能组件

function HomeScreen(props:Props) {
return (...)
}
HomeScreen.navigationOptions = {...} // VS Code error here

问题是我错过了 static navigationOptions: any 属性/方法,并且得到了错误 [在 VS Code 中]

'navigationOptions' does not exist on type '(props: Props) => Element'

有没有办法在函数组件中拥有静态属性/方法?或者有什么解决方案吗?

最佳答案

这可能有点晚了。你的功能组件就像

export default function HomeScreen(props){
return(..)
}
//the following is the static field from class component
HomeScreen.navigationOptions={
title:'Home',
};

我希望这可以帮助某人。

关于reactjs - 函数组件中的 React/ReactNative 静态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57404683/

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