【发布时间】:2021-05-05 17:08:12
【问题描述】:
我使用const 以便我可以为我的屏幕使用navigation 道具。现在,我需要实现componentDidMount(),但我必须切换到一个类来做到这一点。我怎样才能拥有它,以便我可以同时拥有 navigation 道具和类 Component 功能?
代码示例:
Navigation.ts
import { ParamListBase, RouteProp } from "@react-navigation/native";
import { StackNavigationProp } from '@react-navigation/stack';
export interface StackNavigationProps<
ParamList extends ParamListBase,
RouteName extends keyof ParamList = string
> {
navigation: StackNavigationProp<ParamList, RouteName>;
route: RouteProp<ParamList, RouteName>;
onPress: () => void;
}
export type Routes = {
Screen1: undefined;
Screen2: undefined;
Screen3: undefined,
};
Screen1.tsx
const Screen1 = ({ navigation }: StackNavigationProps<Routes, "Screen1">) => {
...
}
如何将上面和下面的内容结合起来以包含navigation props 和React.Component?
class Screen1 extends React.Component {
componentDidMount() {
...
}
}
【问题讨论】:
-
你可以使用 useEffect 钩子来做同样的任务,否则类会以同样的方式获取道具,尝试使用 this.props.navigation
标签: javascript typescript react-native class react-props