首页 > 解决方案 > 显示来自 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 文件的数据

//

标签: reactjs

解决方案


尝试使用

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 );

推荐阅读