【发布时间】:2020-06-27 11:23:51
【问题描述】:
我收到上述错误,我不知道如何处理。
我有一个组件。在 render() 中,我循环遍历一个数组并放置另一个组件并将值解析给该组件,如下所示:
render() {
let allProducts = this.state.products.map((product, i) => {
return (
<div key={product.article}>
...
<PriceStock value={product.article} />
...
</div>
)
})
}
在 PriceStock 组件中,我使用 axios 获取一些数据,如下面的代码:
export default class PriceStock extends React.Component {
constructor(props) {
super(props);
this.state = ({
buttoprice: ''
})
this.getPriceAndStock = this.getPriceAndStock.bind(this)
}
getPriceAndStock(articleNo) {
return axios.post('LINK_TO_URL', {
articleNo: articleNo
}).then(result => {
return result.data
})
}
async componentDidMount() {
let pricestock;
pricestock = await this.getPriceAndStock(this.props.value)
let bruttoPrice = PRICE_TO_PARSE_TO_THE_STATE;
this.setState({ buttoprice: bruttoPrice })
}
render() {
return (
<div >
{this.state.buttoprice}
</div>
);
}
}
当我尝试在componentDidMount中设置状态时似乎发生了错误,有什么建议吗?
【问题讨论】:
-
你需要从你的父组件中加载所有数据并作为props发送给chid
-
我不明白,PriceStock 组件需要知道确切的父组件才能出现在正确的 div 中,我一直没有处理反应那么久抱歉:-9
-
我已经查看了你的两个链接,但我还没有修复它:-S
标签: reactjs