我在react-native开发中使用typescript。我将params传递给navigate
函数进行筛选。
this.props.navigation.navigate("NextScreen", { title: "title" })
在NextScreen
我通过this.props.navigation.state.params.title
访问参数,但我params
出现了tslint错误。
TS2339:Property 'params' does not exist on type 'NavigationState'.
这是一些代码。
import { NavigationInjectedProps } from "react-navigation";
interface OwnProps {
}
interface OwnState {
}
type Props = NavigationInjectedProps & OwnProps;
class NextScreen extends React.Component<Props, OwnState> {
...
public render() {
// tslint error occurs in this line.
const { title } = this.props.navigation.state.params;
...
}
}
我假设我应该定义传递道具的类型但是什么是正确的方法?
答案 0 :(得分:3)
navigation
重新定义NavigationScreenProp<NavStateParams>
。
import { NavigationInjectedProps, NavigationScreenProp, NavigationState } from "react-navigation";
...
interface ParamType {
title: string;
}
interface StateParams extends NavigationState {
params: ParamType;
}
interface OwnProps {
navigation: NavigationScreenProp<StateParams>;
}
type Props = OwnProps & NavigationInjectedProps;
答案 1 :(得分:0)
像这样提取params
const {params} = this.props.navigation.state;
const title = params.title