reactjs - 导航到组件时声明为接口的道具不可用
问题描述
我有组件,其中道具被声明为接口,并且该组件将类型作为该接口。
例如->
import React, { Component } from 'react'
import { Text, View } from 'react-native'
interface IDummy{
name:string;
age:number;
}
export default class Dummy extends Component<IDummy,any> {
constructor(props:IDummy){
super(props)
console.log('CHeck the props here ',props)
}
render() {
return (
<View>
<Text> Just Text </Text>
</View>
)
}
}
现在,如果我将该组件用作其他组件的一部分,例如
<Text>Something</Text>
<Dummy/>
道具名称和年龄工作正常。但是当我想导航到那个组件时,
navigation.navigate('Dummy')
界面中提到的道具不可用。有什么办法可以访问它们???
解决方案
我会假设你正在使用react-navigation
为了发送参数,您应该使用以下行进行导航:
navigation.navigate('Dummy', {
name: 'name',
age: 'age',
});
发送导航事件后,您可以读取以下值:
import React, { Component } from 'react'
import { Text, View } from 'react-native'
interface IDummy{
name: string;
age: number;
route: any;
}
export default class Dummy extends Component<IDummy,any> {
constructor(props: IDummy){
super(props)
console.log('properties from navigation ', props.route.params);
console.log('Check the props here ',props)
}
render() {
return (
<View>
<Text> Just Text </Text>
</View>
)
}
}
有关它的更多信息:将参数传递给路由
推荐阅读
- python - 如何在 python 中使用 np.zeros 作为迭代器?
- c++ - 尝试使用二维数组
- python - 从 numpy 数组中删除类似方波的伪影
- database - 如何在 Oracle SQL Plus 中排列或格式化表结果
- java - 为Jbutton的图标设置不同的背景颜色
- android - 在片段类中使用字符串数组的 TextView 的 SetText
- javascript - Vue.js CLI 中的多个页面
- google-admin-sdk - Google Admin SDK - 在现有用户上创建 posix 属性
- java - 启动层初始化时出错 java.lang.module.FindException: Module jdk.compiler not found
- java - RadixSort 空指针