首页 > 解决方案 > 我如何获得这个 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;

标签: react-nativereact-propses6-class

解决方案


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
 )
}

推荐阅读