我正试图使用AWS Amplify的DataStore和@react navigation/nature库中的useRoute((钩子导入视频ID。
到目前为止,一切都在工作,但我一直收到这个错误:
Property 'id' does not exist on type 'object'.
我试着在谷歌上搜索,发现了一些类似的东西,但没有一个解决方案适用于我的应用程序。
我的代码:
const [video, setVideo] = useState<Video | null >(null)
const [comments, setComments] = useState<Comment[]>([])
const route = useRoute()
const videoId = route.params?.id
useEffect(() => {
DataStore.query(Video, videoId).then(setVideo)
}, [videoId])
react-navigation
的useRoute
需要提供显式类型参数才能键入返回的结果。
首先,您需要在某个地方定义应用程序的完整路由列表和相应的路由属性:
type AppRouteParamList = {
Home: undefined // undefined means Home route doesn't have route parameters
Video: { id: string } | undefined
}
然后在您的组件中,您必须使用正确的类型参数对useRoute
进行注释:
import { AppRouteParamList } from './routes.ts'
import { useRoute, RouteProp } from '@react-navigation/native'
const route = useRoute<RouteProp<AppRouteParamList, 'Video'>>()
const videoId = route.params?.id