gpt4 book ai didi

javascript - 类型错误 : Cannot read property 'map' of undefined on React. js

转载 作者:行者123 更新时间:2023-11-30 13:52:50 27 4
gpt4 key购买 nike

我正在按照有关受控输入的 React.js 在线教程进行操作,但我反复收到错误

TypeError: Cannot read property 'map' of undefined

import CallRow from "./CallRow";
import React from "react";

class SearchPage extends React.Component {
constructor() {
super();
this.state = {
search: "Level Up"
};
}

updateSearch(event) {
this.setState({ search: event.target.value.substr(0, 20) });
}

render() {
return (
<div>
<ul>
{this.props.calls.map(call => (
<CallRow call={call} key={call.id} />
))}
</ul>
<input
type="text"
value={this.state.search}
onChange={this.updateSearch.bind(this)}
/>
</div>
);
}
}

export default SearchPage;

最佳答案

好像是call prop undefined.

在映射 prop 值之前,检查它是否未定义。

请引用以下代码:

<ul>
{(this.props.calls || []).map(call => (
<CallRow call={call} key={call.id} />
))}
</ul>

关于javascript - 类型错误 : Cannot read property 'map' of undefined on React. js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57897520/

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