这个 ES6 Class 中的道具如何获取?
How do I get the props in this ES6 Class?
我尝试使用构造函数 (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;
props 是从 class 个组件中的 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
)
}
我尝试使用构造函数 (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;
props 是从 class 个组件中的 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
)
}