![](/img/trans.png)
[英]React functional component TypeError: Cannot read property 'props' of undefined
[英]Cannot read property 'props' of undefined in functional component
所以我有以下反应功能组件:
import React from "react";
import LNSelect from "../LNSelect/LNSelect";
import { CountryRegionData } from "react-country-region-selector";
const parsed = CountryRegionData.map(
([country_name, country_code, cities]) => ({
country_name,
country_code,
cities: cities
.split("|")
.map(cityData => cityData.split("~"))
.map(([city_name, city_code]) => ({ city_name, city_code }))
})
);
const regions = parsed
.find(country => country.country_name === this.props.country)
.cities.map(({ city_name, city_code }) => ({
label: city_name,
value: city_code
}));
const LNSelectRegion = props => {
return <LNSelect options={regions} {...props} />;
};
export default LNSelectRegion;
基本上我使用 package 来获取国家和他们的城市/城市代码,然后找到一个作为支柱country
传递的特定国家并将其城市映射到label
和value
数组以用于我有的选择器组件,这个代码的问题是在.find
行上,我收到错误Cannot read property 'props' of undefined
,我想解决这个问题,我还想为 country 属性设置一个默认值,以防它为空( if this.props.country === "" {this.props.country = "United States}
),我该怎么做?
1)在功能组件中,您可以使用props
而不是this.props
获取道具。
2)您只能在功能组件LNSelectRegion
中获取道具。
所以我只是在这个标准上重新编写你的代码,希望它会起作用
import React from "react";
import LNSelect from "../LNSelect/LNSelect";
import { CountryRegionData } from "react-country-region-selector";
const parsed = CountryRegionData.map(
([country_name, country_code, cities]) => ({
country_name,
country_code,
cities: cities
.split("|")
.map(cityData => cityData.split("~"))
.map(([city_name, city_code]) => ({ city_name, city_code }))
})
);
const LNSelectRegion = props => {
const regions = parsed
.find(country => country.country_name === props.country)
.cities.map(({ city_name, city_code }) => ({
label: city_name,
value: city_code
}));
return <LNSelect options={regions} {...props} />;
};
export default LNSelectRegion;
你不能在功能组件中使用this
,所以你的代码应该是:
const regions = parsed
.find(country => country.country_name === props.country)
.cities.map(({ city_name, city_code }) => ({
label: city_name,
value: city_code
}));
您还需要 function 并在此块周围传递 props 参数
const YourComponent = (props) => {
// your code above
}
const regions
function 应该写在传递 props 的功能组件内。
// Functional component
const Child = props => {
const regions = parsed
.find(country => country.country_name === props.country)
.cities.map(({ city_name, city_code }) => ({
label: city_name,
value: city_code
}));
return (
<div></div>
)
}
因为this.props.country
中的this
是未定义的。 尝试通过 prop 获取regions
const getRegions = (country) => parsed
.find(country => country.country_name === country)
.cities.map(({ city_name, city_code }) => ({
label: city_name,
value: city_code
}));
const LNSelectRegion = props => {
return <LNSelect options={getRegions(props.country)} {...props} />;
};
您的parsed
和regions
常量在实际组件之外,您无法在那里访问道具。 功能组件中也没有“this”。 看看下面的代码:
import React from "react";
import LNSelect from "../LNSelect/LNSelect";
import { CountryRegionData } from "react-country-region-selector";
const parsed = CountryRegionData.map(
([country_name, country_code, cities]) => ({
country_name,
country_code,
cities: cities
.split("|")
.map(cityData => cityData.split("~"))
.map(([city_name, city_code]) => ({ city_name, city_code }))
})
);
const getRegions = (countryFromProps) => parsed
.find(country => country.country_name === countryFromProps)
.cities.map(({ city_name, city_code }) => ({
label: city_name,
value: city_code
}));
const LNSelectRegion = (props) => {
return <LNSelect options={getRegions(props.country)} {...props} />;
};
LNSelectRegion.defaultProps = {
country: "United States"
}
export default LNSelectRegion;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.