react-native - 我如何获得这个 ES6 Class 的道具?
问题描述
我尝试使用构造函数(道具),但仍然无法找到变量:道具热我得到了这个道具:
export default class DetailScreen extends React.Component {
state = {
value: '399',
checked: 'Pan Crust',
};
render() {
const productId = props.navigation.getParam('productId');
constselectedProduct = useSelector((state) =>
this.state.products.vegProducts.find((prod) => prod.id === productId),
);
const {checked} = this.state;
解决方案
props 从this
类组件中访问。
所以,渲染函数的第一行应该是
const productId = this.props.navigation.getParam('productId');
更新 反应导航 5.x
您应该能够像这样访问路由参数
const productId = this.props.route.params.productId;
钩子应该用在功能组件中。你应该重构你的代码看起来像这样
export const DetailScreen = ({route)} => {
const [value, setValue] = useState('399');
const [checked, setChecked] = useState('Pan Crust');
const productId = route.params.productId;
return (
// return view here
)
}
推荐阅读
- reactjs - react-testing-library 渲染 VS ReactDOM.render
- c# - 遍历时计算嵌套列表对象
- javascript - 具有动态字段的 Angular 反应式表单
- css - pug 文件,连接到 Websocket,然后显示到达的值?
- c# - FileNotFoundException:无法加载文件或程序集'System.Configuration.ConfigurationManager,
- python - 为什么我的 TKinter GUI 代码可以在交互式 shell 中工作,但从文件运行时却不行?
- gcc - 请使用支持 __attribute__((constructor)) 的编译器
- oracle - 如何在每月 15 日后的 5 个工作日安排 Oracle 作业?
- elasticsearch - Elasticsearch 7.5 使用什么函数计算默认分数?
- r - 将 .sql 文件传递到 DBI::dbGetQuery 中,并将注释作为第一行传递到 R / RStudio