【发布时间】:2020-12-21 13:28:27
【问题描述】:
我有 responsive.tsx 文件,我想将一个 useState 挂钩作为道具,其中包含来自 app.tsx 的应用程序的方向模式。我对类型有疑问。
“Dispatch
//in responsive.tsx
type OrientationProp = {
orientation:string
}
type Dispatcher<S> = Dispatch<SetStateAction<S>>;
type Props = {
setOrientation : Dispatcher<any>
}
const listenOrientationChange = ({ setOrientation }:Props) => {
Dimensions.addEventListener("change", (newDimensions) => {
// Retrieve and save new dimensions
screenWidth = newDimensions.window.width;
screenHeight = newDimensions.window.height;
// Trigger screen's rerender with a state update of the orientation variable
});
let orientation:OrientationProp = {
orientation: screenWidth < screenHeight ? "portrait" : "landscape",
};
setOrientation(orientation);
};
//in app.tsx
const [orientation,setOrientation] = useState(null);
useEffect(() => {
listenOrientationChange(setOrientation) // the error is here //Argument of type 'Dispatch<SetStateAction<null>>' is not assignable to parameter of type 'Props'. Property 'setOrientation' is missing in type 'Dispatch<SetStateAction<null>>' but required in type 'Props'
},[])
【问题讨论】:
-
请指定“类型问题”,但不要在代码 cmets 中。它会更具可读性。
-
感谢您的建议。你能帮我吗?
标签: typescript react-native types