我如何获得此ES6类中的道具?

问题描述 投票:0回答:1

我尝试使用构造函数(props),但仍然无法找到变量:props我能从中得到道具吗?

    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-native react-props es6-class
1个回答
0
投票

属性从类组件中的this访问。

因此,render函数的第一行应该是

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
 )
}
© www.soinside.com 2019 - 2024. All rights reserved.