gpt4 book ai didi

javascript - React TypeError : _this2. props.variations.find 不是函数

转载 作者:行者123 更新时间:2023-12-03 00:44:52 26 4
gpt4 key购买 nike

不知道为什么会出现此错误。我需要遍历变体并找到包含 varid 变量的 id。对我来说,这看起来是对的,但显然不是。我相信这里的每个人都比我聪明得多,哈哈,我在这方面仍然是个新手。

此函数应该允许我过滤状态,以便我只拥有所需的数据,并且可以在页面中显示该数据,而不是包含所有 drupal 产品的状态。也许还有一种更有效的方法来做到这一点,我不确定。

代码如下:

class ProductPage extends Component {
constructor(props) {
super(props);

this.toggle = this.toggle.bind(this);
this.state = {
dropdownOpen: false
};
}

toggle() {
this.setState(prevState => ({
dropdownOpen: !prevState.dropdownOpen
}));
}

render() {
let style = {
height: this.props.height - 56,
};

let product = this.props.products.items.find(o => o.path[0].alias === this.props.router.match.url);
console.log(product);
console.log(this.props.variations);

let variationList = [];

if (product && this.props.variations) {
for (let i = 0; i < product.variations.length; i++) {
let varid = product.variations[i].variation_id;
let variation = this.props.variations.find(o => o.path[0].alias === varid);
variationList.push(variation);
}
}

let body = product && product.body.length ? product.body[0].value : null;

return (
<div className="App" id="default">
<div className='MenuBar'>
<MenuBar/>
</div>
<div>
<div style={style} className="ProductPage row no-gutters">
<div className="col-xs-3 col-md-3">
<LeftMenuBar/>
</div>
<div className="outer col-xs-4 col-md-4">
<div>
<div id="ProductPlacement">
<img src={WomensWear} alt=""/>
<div id="alternate-pics">
<div id="alt-pic">
</div>
<div id="alt-pic">
</div>
<div id="alt-pic">
</div>
</div>
</div>
</div>
</div>
<div className="col-xs-5 col-md-5">
<div id="ImagePlacement">
<div className="ProductTitle">
<h1>First Product</h1>
</div>
<hr/>
<div className="ProductDescription">
<div dangerouslySetInnerHTML={{__html: body}} />
</div>
<div id="options">
<div id="color">
</div>
<div id="color2">
</div>
<div id="color3">
</div>
</div>
<div id="options">
<div>
<Dropdown isOpen={this.state.dropdownOpen} toggle={this.toggle}>
<DropdownToggle caret id="size-dropdown">
Size
</DropdownToggle>
<DropdownMenu>
<DropdownItem>1</DropdownItem>
<DropdownItem>3</DropdownItem>
<DropdownItem>5</DropdownItem>
</DropdownMenu>
</Dropdown>
<div className="AddToCart">
<button className="AddToCart">Add To Cart</button>
<button className="Price">$34.99</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
);
}
}

export default ProductPage;

最佳答案

我也遇到了这个问题,通过反复试验发现这是因为复制+粘贴。检查您的化简器,确保您没有设置对象 {} 或其他类型的默认值,而不是数组 []

就我而言,我有(顺便说一句,这是 ES6 语法):

const someReducer = (state = {}, action) => {
switch (action.type) {
case 'reducer type':
// get the data etc.
return [];
default:
return state;
}
};

当我应该有:

const someReducer = (state = [], action) => {
switch (action.type) {
case 'reducer type':
// get the data etc.
return [];
default:
return state;
}
};

请注意我设置默认值的第一行。这是在 api 调用挂起时使用的,因此如果恰好到达正在使用 .find() 的代码,那么您将收到错误。

这一点非常容易被忽视,因为数据检索速度如此之快,以至于您无法轻易看出数据何时是错误类型。无论您如何设置,只要确保您的默认类型是正确的即可!希望对某人有所帮助!

关于javascript - React TypeError : _this2. props.variations.find 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53292435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com