javascript - 反应地图不是我的应用程序中的功能错误
问题描述
我是 ReactJS 的初学者,我遇到了一个我不明白的错误。这是我第一次使用 ReactJS 编写的应用程序。这是我的代码。
错误
react map is not a function error in my app
搜索栏组件
import RecipeList from './recipes_list';
class SearchBar extends Component {
state = {
term : []
}
onInputChange(term){
this.setState({term});
}
onSubmit = async (term) => {
const recName= this.state.term;
term.preventDefault();
const api_key = 'a21e46c6ab81bccebfdfa66f0c4bf5e9';
const api_call = await Axios
.get(`https://www.food2fork.com/api/search?key=${api_key}&q=${recName}&`)
.then(res=> {this.setState({term : res.data.recipes})})
}
render() {
return (
<div>
<form onSubmit={this.onSubmit} >
<div className="search-bar">
<input
type="text"
value={this.state.term}
onChange={event => this.onInputChange(event.target.value)}
/>
<button type="submit">Search</button>
</div>
</form>
<RecipeList List ={this.state.term}/>
</div>
)
}
}
食谱列表组件
const RecipeList = props => (
<div>
{
props.List.map((recipe) => {
return (
<div>{recipe.title}</div>
)
})
}
</div>
)
export default RecipeList;
谢谢你们的帮助
解决方案
您的问题在此代码段中:
onInputChange(term){
this.setState({term});
}
这会将您的状态变量设置为字符串。例如,如果我输入Hello!
,您的状态对象将是{ term: 'Hello!' }
。你现在试图.map()
通过一个字符串,String.map
不是一个函数。
推荐阅读
- javascript - 如果 JSON 对象为空,则将空数组发布到 API
- assembly - MASM x64 中的跳转表实现?
- bash - 为什么 Anaconda Cloud 上有多个安装命令?
- vba - 如何获取谷歌翻译的“text-area”元素的ID?
- rust - 从另一个线程取消互斥锁内的分布式锁会导致死锁
- microsoft-graph-api - C# graph SDK - 序列化批处理请求的结果
- android - 更换手机后如何恢复 RSA 密钥?
- php - 校正后,这显示下面给出的结果
- linux - 如何使用 awk 去除输出的最后一列字符?
- android - 如何加载这个html并将值发送到webview android中的post方法