TypeError: Cannot read property 'onSelect' of undefined
TypeError: Cannot read property 'onSelect' of undefined
我在 React 中创建了一个 class 组件,需要在映射时在渲染函数中传递道具,但我收到以下错误 TypeError: Cannot read property 'onSelect' of undefined
下面是我的组件
class SelectLanguage extends Component {
render() {
let filterbrands = this.props.tvfilter.brand
const filteredList = this.filterItems();
if (filterbrands) {} else {}
return (
<div className="filter-options">
{filteredList &&
filteredList.map(item => (
<dl>
<dt className="mb-2">{item.title}</dt>
<input type="search" onChange={this.onSearchInputChange} className="search mb-2" placeholder="Search by Brand" />
{item.options.slice(0,this.state.itemsToShow).map(catname => (
<dd
key={catname.catgeory_name}
className="mb-0"
>
<a href="#" onClick={props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
</dd>
))}
</dl>
))}
<a className="btn btn-primary expandbutton" onClick={this.showMore}>{this.state.expanded ? (
<span>[ - less ]</span>
) : (
<span>[ + more ]</span>
)
}</a>
</div>
);
}
// PropTypes for SelectedLanguage
SelectLanguage.PropTypes = {
selectedLanguage: PropTypes.string.isRequired,
onSelect: PropTypes.func.isRequired
};
}
问题出在这个锚<a href="#" onClick={props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
注意:如果我使用函数而不是像这样 class function SelectLanguage(props) {
,它会起作用
我相信您在将函数设置为之前错过了 "this"
<a href="#" onClick={this.props.onSelect.bind(null, item)}>
你在props.onSelect之前少了"this";
您可以在渲染顶部定义道具以提高可读性;
例如 const {onSelect } = this.props
请更改此代码:
<a href="#" onClick={props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
到
<a href="#" onClick={this.props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
我在 React 中创建了一个 class 组件,需要在映射时在渲染函数中传递道具,但我收到以下错误 TypeError: Cannot read property 'onSelect' of undefined
下面是我的组件
class SelectLanguage extends Component {
render() {
let filterbrands = this.props.tvfilter.brand
const filteredList = this.filterItems();
if (filterbrands) {} else {}
return (
<div className="filter-options">
{filteredList &&
filteredList.map(item => (
<dl>
<dt className="mb-2">{item.title}</dt>
<input type="search" onChange={this.onSearchInputChange} className="search mb-2" placeholder="Search by Brand" />
{item.options.slice(0,this.state.itemsToShow).map(catname => (
<dd
key={catname.catgeory_name}
className="mb-0"
>
<a href="#" onClick={props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
</dd>
))}
</dl>
))}
<a className="btn btn-primary expandbutton" onClick={this.showMore}>{this.state.expanded ? (
<span>[ - less ]</span>
) : (
<span>[ + more ]</span>
)
}</a>
</div>
);
}
// PropTypes for SelectedLanguage
SelectLanguage.PropTypes = {
selectedLanguage: PropTypes.string.isRequired,
onSelect: PropTypes.func.isRequired
};
}
问题出在这个锚<a href="#" onClick={props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
注意:如果我使用函数而不是像这样 class function SelectLanguage(props) {
我相信您在将函数设置为之前错过了 "this"
<a href="#" onClick={this.props.onSelect.bind(null, item)}>
你在props.onSelect之前少了"this";
您可以在渲染顶部定义道具以提高可读性;
例如 const {onSelect } = this.props
请更改此代码:
<a href="#" onClick={props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>
到
<a href="#" onClick={this.props.onSelect.bind(null, item)}>{catname.catgeory_name}</a>