reactjs - 显示来自 API 的数据
问题描述
我在 parent.js 文件中有以下代码,它从 API 获取数据使用axios
它工作正常
//父.js
componentDidMount() {
axios.get('URL', {
method: 'GET',
headers: {
'key': 'apikeygoeshere'
}
})
.then((response) => {
this.success(response);
})
}
successShow(response) {
this.setState({
person: response.data.data.Table
});
}
render() {
return (
<div class="row">
{this.state.person.map(results => (
<h5>{results.first_name}</h5>
)
)
}
上面的代码完美地显示了来自 api 的数据。我想在子组件中显示 api 数据,而不是显示来自 json 文件的数据。在子组件中,我有以下代码显示来自 local.json 文件的数据
//
解决方案
尝试使用
const data = this.state.person && this.state.person.find(item => item.id === id);
const relatedTo = this.state.person && this.state.person.filter( item => item.manager_id === data.manager_id && item.id !== data.id );
推荐阅读
- r - 更改 jupyter notebook 主题以突出显示 R 功能
- javascript - 如何使用 Selenium 从页面滚动特定 div?
- android - 由于类型不匹配,无法在 Flow 中发出 Either.Left
- typescript - Vue 3 使用 v-model 作为 props 而不是 :prop 和 @emit
- javascript - 为什么在某些情况下“鼠标悬停”不能模拟悬停?
- java - 如何在 netbeans 12.4 中启用本机打包
- pine-script - 如何在松纸上玩弄 SMA 的时间框架?
- amazon-web-services - 在 AWS ElasticBeanstalk 上使用 .ebextensions 为 SQL Server 安装 Microsoft ODBC Driver 17 时出现问题
- mysql - 如何选择缺少特定类型行的记录
- c# - 我如何遍历字符串数据类型的日期列表以将每个日期传递给 sqlcommand 以将表中的行获取到 datagridview