Sea*_*ene 6 typescript reactjs react-native react-navigation
我正在尝试使用react-native,react-navigation和Typescript一起创建一个应用程序。总共只有两个屏幕(HomeScreen
和ConfigScreen
)和一个组件(GoToConfigButton
),如下所示。
import React from "react";
import { NavigationScreenProps } from "react-navigation";
import { Text, View } from "react-native";
import GoToConfigButton from "./GoToConfigButton";
export class HomeScreen extends React.Component<NavigationScreenProps> {
render() {
return (
<View>
<Text>Click the following button to go to the config tab.</Text>
<GoToConfigButton/>
</View>
)
}
}
Run Code Online (Sandbox Code Playgroud)
import React from "react";
import { Button } from "react-native";
import { NavigationInjectedProps, withNavigation } from "react-navigation";
class GoToConfigButton extends React.Component<NavigationInjectedProps> {
render() {
return <Button onPress={this.handlePress} title="Go" />;
}
private handlePress = () => {
this.props.navigation.navigate("Config");
};
}
export default withNavigation(GoToConfigButton);
Run Code Online (Sandbox Code Playgroud)
ConfigScreen
未提供的代码,因为在这里不重要。好了,上面的代码实际上起作用了,我可以通过单击按钮转到配置屏幕。问题是,Typescript认为我应该手动提供该navigation
属性GoToConfigButton
。
<View>
<Text>Click the following button to go to the config tab.</Text>
<GoToConfigButton/> <-- Property "navigation" is missing.
</View>
Run Code Online (Sandbox Code Playgroud)
如何告诉Typescript该navigation
属性是由自动赋予的react-navigation
?
您只是缺少包装您的 NavigationInjectedProps 的 Partial<> 接口,正如修复此问题的拉取请求中所描述的那样。
import React from "react";
import { Button } from "react-native";
import { NavigationInjectedProps, withNavigation } from "react-navigation";
class GoToConfigButton extends React.Component<Partial<NavigationInjectedProps>> {
render() {
return <Button onPress={this.handlePress} title="Go" />;
}
private handlePress = () => {
this.props.navigation.navigate("Config");
};
}
export default withNavigation(GoToConfigButton);
Run Code Online (Sandbox Code Playgroud)
使用@types/react-navigation >= 2.13.0进行测试
归档时间: |
|
查看次数: |
3649 次 |
最近记录: |